【问题标题】:Can we exclude setting http header Content-type:application/json from the request?我们可以从请求中排除设置 http 标头 Content-type:application/json 吗?
【发布时间】:2013-12-05 17:27:13
【问题描述】:

我正在尝试使用 Chrome 中的 Postman 插件创建邮递员收集请求。

请求的预览如下:

PUT /api/20130409/system/users/618a9ff389bc4bcda22e20150f818d78 HTTP/1.1
Host: 127.0.0.1:81
Content-Type: application/json
Cache-Control: no-cache

{ "User": { "UserName": "updated1", } }

然后,我从请求中删除了Content-Type 标头,并将ServiceStack 的DefaultContentType 属性设置为“application/json”。

问题一:

ServiceStack 是否应该将请求内容解释为 JSON ? (它在我的盒子上失败了)

我的理解是DefaultContentType 是关于在请求上设置“接受”标头。如果请求没有设置带有特定值的“Accept”标头,例如 "Application/json""Application/xml",则响应内容将默认为属性 DefaultContentType 上设置的值。

如果我上面所说的理解是正确的……

问题2:

请求是否总是需要在PUT/POST 场景中设置正确的Content-Type 标头?

【问题讨论】:

    标签: servicestack


    【解决方案1】:

    我不确定默认的内容类型,但您可以在主机配置中修改和分配您想要的内容类型,尝试以下操作:

     DefaultContentType = ContentType.Json
    

    你也可以选择xml。

    【讨论】:

    • 我以编程方式设置 SetConfig( new EndpointHostConfig { DefaultContentType = ContentType.Json });但是我仍然需要在我的请求中添加 Content-Type 标头,否则会失败
    • @RajYennam 还可以在路由 wiki 上查看 Content Negotiation Section,了解请求不同内容类型的替代方法。
    猜你喜欢
    • 2012-12-18
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多