c# - ServiceStack Client Put请求和查询参数

标签 c# servicestack

我在 .Net 项目中使用最新的 ServiceStack 客户端库,但在发出 PUT 请求时遇到一些问题。 特别是它似乎没有考虑 RequestDto 对象中定义的参数类型并将所有参数放入正文中(尽管参数被定义为 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 手动运行查询(并将两个参数放入查询字符串中)时,它工作正常。

当使用 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 请求,在这种情况下,您应该 disable WebDav这样请求就可以畅通无阻地到达ServiceStack。

要调试此类 HTTP 互操作性问题,您应该使用类似 Fiddler 的工具检查(并在此处提供)原始 HTTP 响应 header 、Chrome Web 检查器或 WireShark 。如果 HTTP 响应 header 不包含 X-Powered-By: ServiceStack.. header ,则请求可能在到达 ServiceStack 之前已被拦截并阻止,例如IIS/ASP.NET 或转发代理。

The client code is setting the parameters in the body while they're supposed to be in the query.

ServiceStack 仅在没有请求正文(如 GETDELETE)的 HTTP 动词的正文中发送参数,对于具有请求正文的动词,例如POSTPUT ServiceStack 的 JsonServiceClient 将按预期 POST JSON。

ServiceStack 服务将接受以 QueryString、JSON 请求正文或 x-www-form-urlencoded Content-Type 形式发布的参数。如果您不调用 ServiceStack 服务,您应该 using a generic HTTP ClientHTTP Utils这将允许您控制 exactly how the Request is sent ,例如:

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

将以 x-www-form-urlencoded 形式发送结果。

ServiceStack's .NET Service Clients仅用于向 ServiceStack 服务发送请求。

关于c# - ServiceStack Client Put请求和查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42916790/

相关文章:

rest - 是否可以为 ServiceStack 创建的 REST API 自动生成好的文档?

c# - Azure AD 获取错误的 token 版本

c# - Server.transfer 回发问题

c# - 如何使用 string.join 连接对象数组中的值?

c# - Windows 服务是否是 "correct"与必须运行 Windows 的嵌入式系统上的硬件交互的选择?

c# - 如何在 WinForms 窗口中显示 PDF 或 Word 的 DOC/DOCX?

dependency-injection - ServiceStack - 按名称注入(inject)存储库

servicestack - protobuf-net 属性索引器

typescript - 对象不支持属性或方法 'json' servicestack-client HTTP post

asp.net - AngularJs + ServiceStack 应用程序的安全性