为什么设置POST内容类型:"Application/Json"导致“错误请求".在REST WebService上? [英] Why Setting POST Content-type:"Application/Json" causes a "Bad Request" on REST WebService?

查看:235
本文介绍了为什么设置POST内容类型:"Application/Json"导致“错误请求".在REST WebService上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用我的Web服务时遇到一个奇怪的问题:

I've a strange problem using my web service :

当我作为客户端在不设置任何类型内容"标头的情况下将表单发布到REST Web服务时,一切正常.我在服务器上接到电话,并且Json文本可用.

When I, as a client, POST a form to the REST Web Service without setting any "type-content" header, things works fine. I get the call on the server and the Json text is available.

如果我作为客户端将我的发布请求的内容类型标头设置为"application/json",则服务器会通过错误请求(400)"进行回复.

If I, as a client, sets the content-type header of my post request to "application/json", the server reply by a "Bad Request (400)".

Web服务方法是:

 <WebInvoke(UriTemplate:="Login", Method:="POST")>
 Public Function LoginFormAccess(data As IO.Stream) As String
 (...)
 end function

如果我不提及"Application/Json",则可以检索发布在 data 流中的客户端json类型的正文.如果这样做,我会得到错误请求的答案.

If I don't mention "Application/Json", I can retrieve the client json-typed body, posted in the data stream. If I do, I get the Bad Request answer.

为什么关于内容类型值的行为有如此大的差异,又有什么提供这种差异?该方法是否需要其他类型的参数,还是必须更改或检查其他内容?

Why is there a such great difference of behavior concerning the content-type value and what is provide such a difference ? Does the method needs another type of parameter, or something else has to be changed or checked?

已添加:如果内容类型为"Application/Json",并且主体为空,则可以正常工作...!

  • 注释1:实例化负责应答呼叫的班级. (一种 跟踪new()证明了这一点).因此,问题似乎在于服务器未找到合适的方法来调用该对象...

  • Note 1 : The class responsible to answer the call is instantiated. (A trace in the new() proves it). So, the problem seems to be that the server does not find a proper method to call on that object...

注2:我在使用不同的客户端时遇到了这个问题,所以我很漂亮 当然是服务器端问题.

Note 2 : I've this problem using different clients, so I'm pretty sure is a server-side problem.

注3:我使用了WCF REST服务应用程序模板来创建 我的项目.

Note 3 : I've used a WCF REST Service Application Template to create my project.

注释4:提及"ResponseFormat = WebMessageFormat.Json"和"RequestFormat = WebMessageFormat.Json"无济于事.

Note 4 : Mentioning "ResponseFormat=WebMessageFormat.Json" and "RequestFormat=WebMessageFormat.Json" does not help.

推荐答案

content-type标头添加到请求时,您将在您的数据中指定数据的内容类型请求,而不是您希望看到的内容返回.

When you add a content-type header to your request, you are specifying the content type of the data in your request, not the content you expect to see returned.

根据您发布的内容,我敢打赌,您正在将XML发布到服务,并且当将content-type设置为JSON时,将破坏服务器解析正文的能力.您可能会模棱两可的声明如果内容类型为"Application/Json"而主体为空,则可能会证实这一点(假设您的意思是请求的主体,而不是响应的主体).

My bet from what you've posted is that you are posting XML to the service, and when you set content-type to JSON you are breaking the server's ability to parse the body. Your ambiguous statement that it works "If the content-type is "Application/Json" AND the body is empty" might bear this out (assuming you mean the body of your request, not of the response).

您可以通过添加XML而不是JSON的内容类型来证明这一点-如果它与您的请求的实际内容匹配,则可以正常工作.

You could prove this by adding a content type of XML instead of JSON - which, if it matches the actual body of your request, will work just fine.

简而言之:由于您的请求指定的是JSON内容,而您没有发送JSON,因此出现了错误请求"错误.

In short: it's giving a "Bad Request" error because your request is specifying JSON content and you are not sending JSON.

这篇关于为什么设置POST内容类型:"Application/Json"导致“错误请求".在REST WebService上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆