Rabskatran
Rabskatran

Reputation: 2299

Why Setting POST Content-type:"Application/Json" causes a "Bad Request" on REST WebService?

I've a strange problem using my web service :

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.

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)".

The web service method is :

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

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?

ADDED : If the content-type is "Application/Json" AND the body is empty, then it works...!

Upvotes: 2

Views: 6228

Answers (1)

Dan Puzey
Dan Puzey

Reputation: 34218

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.

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).

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.

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

Upvotes: 1

Related Questions