1

我在 ServiceStack 中创建了一个 REST 服务,但是当我使用 REST URL 调用它时,它不会反序列化我将服务发布到我的请求对象中的 JSON。如果我使用默认 URL 或在查询字符串中提供参数,它工作正常。

我的请求对象:

[Route("/myservice", "POST")]
public class MyRequestObject
{
    public string MyString { get; set; }
    public int MyInt { get; set; }
}

我的服务:

public class MyService : Service
{
    public object Post(MyRequestObject request)
    {
        return new
            {
                MyString = request.MyString,
                MyInt = request.MyInt
            };
    }
}

以及我发布到服务的 json:

{"myString": "abc", "myint": 42}

我的 REST URL 是 /api/myservice,默认 URL 是 /api/json/asynconeway/MyRequestObject。我已验证服务 get 是使用 REST 和默认 URL 调用的。

非常感谢您的意见。

成功标头

要求

POST /api/json/asynconeway/MyRequestObject HTTP/1.1
Host: bd.local
Connection: keep-alive
Content-Length: 32
Origin: chrome-extension://aejoelaoggembcahagimdiliamlcdmfm
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
Accept: application/json
Accept-Encoding: gzip,deflate,sdch
Accept-Language: da-DK,da;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

回复

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
COMMERCE-SERVER-SOFTWARE: Microsoft Commerce Server, Enterprise Edition
X-Powered-By: ServiceStack/3,914 Win32NT/.NET
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 01 Oct 2012 10:32:37 GMT
Content-Length: 29

错误标头

要求

POST /api/myservice HTTP/1.1
Host: bd.local
Connection: keep-alive
Content-Length: 32
Origin: chrome-extension://aejoelaoggembcahagimdiliamlcdmfm
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
Accept: application/json
Accept-Encoding: gzip,deflate,sdch
Accept-Language: da-DK,da;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

回复

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
COMMERCE-SERVER-SOFTWARE: Microsoft Commerce Server, Enterprise Edition
X-Powered-By: ServiceStack/3,914 Win32NT/.NET
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 01 Oct 2012 10:30:38 GMT
Content-Length: 11
4

1 回答 1

1

问题可能出在进行 POST 的代码中。数据 json 是否字符串化,例如:

JSON.stringify({"myString": "abc", "myint": 42})
于 2013-09-28T18:33:19.140 回答