ServiceStack客户端放置请求和查询参数

我在.Net项目中使用最新的ServiceStack客户端库,并且在发出PUT请求时遇到了一些问题。 特别是它似乎没有考虑到RequestDto对象中定义的参数类型,并将所有参数放入正文中(尽管param被定义为type =“query”)。

我的请求对象(自动生成)如下所示:

[Route("/test/name", "PUT")]
public partial class PutTestName
: IReturn<PutTestNameResponse>
{
    ///<summary>
    ///the user id
    ///</summary>
    [ApiMember(Description = "the user id", ParameterType = "query")]
    public virtual string UserId { get; set; }

    ///<summary>
    ///the name
    ///</summary>
    [ApiMember(Description = "the name", ParameterType = "query")]
    public virtual string Name { get; set; }

}

我打电话是这样的:

_apiClient.Put(new PutTestName(){UserId ="xyz..", Name="Bob"});

我得到“资源未找到”异常作为回报。

当我使用Postman手动运行查询(并将两个参数放在Querystring中)时,它工作正常。

当用fiddler调试C#客户端时,我可以看到没有参数设置为查询字符串,并且它们都传递到正文中。

编辑:这是什么Fiddler Request Raw看起来像:

PUT https://xxxx/test/name HTTP/1.1
User-Agent: ServiceStack .NET Client 4.56
Accept-Encoding: gzip,deflate
Ocp-Apim-Subscription-Key: 783....
Content-Encoding: gzip
Accept: application/json
Content-Type: application/json
Host: xxx.net
Content-Length: 115
Expect: 100-continue
Connection: Keep-Alive

{"UserId":"xxx","Name":"Bob"}

ServiceStack API和我的调用之间有Azure API管理,但我不认为这是问题。 客户端代码正在设置参数,而它们应该在查询中。


如果相同的请求与POST协同工作,那么很可能WebDav已启用并干扰您的PUT请求,在这种情况下,您应禁用WebDav,以便请求可以畅通无阻地到达ServiceStack。

为了调试这样的HTTP互操作性问题,您应该使用Fiddler,Chrome Web Inspector或WireShark等工具检查(并在此处提供)Raw HTTP响应头。 如果HTTP响应头没有包含X-Powered-By: ServiceStack..标头,则可能在请求到达ServiceStack之前被拦截并阻止,例如IIS / ASP.NET或转发代理。

客户端代码正在设置参数,而它们应该在查询中。

ServiceStack只为身体中的HTTP动词发送参数,这些参数没有像GETDELETE这样的请求主体,对于具有请求主体的动词,例如POSTPUT, ServiceStack的JsonServiceClient会按预期JsonServiceClient POST JSON。

ServiceStack服务将接受张贴在QueryString,JSON Request Body或x-www-form-urlencoded Content-Type中的参数。 如果你没有调用ServiceStack服务,你应该使用HTTP Utils这样的通用HTTP客户端,这将允许你准确地控制请求的发送方式,例如:

var response = absoluteUrl.ToPutUrl(new PutTestName {...});

将结果作为x-www-form-urlencoded发送

ServiceStack的.NET服务客户端仅用于向ServiceStack服务发送请求。

链接地址: http://www.djcxy.com/p/68717.html

上一篇: ServiceStack Client Put request and query parameters

下一篇: ServiceStack: Send JSON string instead DTO via POST