Http 415 Unsupported Media Type Error With Json

Http 415 Unsupported Media type error with JSON

Not sure about the reason but Removing lines charset=utf8 from con.setRequestProperty("Content-Type", "application/json; charset=utf8") resolved the issue.

HTTP 415 unsupported media type error when calling Web API 2 endpoint

SOLVED

After banging my head on the wall for a couple days with this issue, it was looking like the problem had something to do with the content type negotiation between the client and server. I dug deeper into that using Fiddler to check the request details coming from the client app, here's a screenshot of the raw request as captured by fiddler:

Fiddler capture of http request from client app

What's obviously missing there is the Content-Type header, even though I was setting it as seen in the code sample in my original post. I thought it was strange that the Content-Type never came through even though I was setting it, so I had another look at my other (working) code calling a different Web API service, the only difference was that I happened to be setting the req.ContentType property prior to writing to the request body in that case. I made that change to this new code and that did it, the Content-Type was now showing up and I got the expected success response from the web service. The new code from my .NET client now looks like this:

req.Method = "POST"
req.ContentType = "application/json"
lstrPagingJSON = JsonSerializer(Of Paging)(lPaging)
bytData = Encoding.UTF8.GetBytes(lstrPagingJSON)
req.ContentLength = bytData.Length
reqStream = req.GetRequestStream()
reqStream.Write(bytData, 0, bytData.Length)
reqStream.Close()
'// Content-Type was being set here, causing the problem
'req.ContentType = "application/json"

That's all it was, the ContentType property just needed to be set prior to writing to the request body

I believe this behavior is because once content is written to the body it is streamed to the service endpoint being called, any other attributes pertaining to the request need to be set prior to that. Please correct me if I'm wrong or if this needs more detail.

Error 415 Unsupported Media Type: POST not reaching REST if JSON, but it does if XML

The issue is in the deserialization of the bean Customer. Your programs knows how to do it in XML, with JAXB as Daniel is writing, but most likely doesn't know how to do it in JSON.

Here you have an example with Resteasy/Jackson
http://www.mkyong.com/webservices/jax-rs/integrate-jackson-with-resteasy/

The same with Jersey:
http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/

POST results in a HTTP 415 Unsupported Media Type response

POST results in a HTTP 415 Unsupported Media Type response

When I tested with your code, I did reproduce the problem.

Through debug, it is found that in the fetch method, the contentType needs to be added to the application/json format to pass the json data, but there is a problem with your writing.

To sove it, just to change the content of headers in fecth as follow:

  headers: { 'Content-Type':  "application/json" }


Related Topics



Leave a reply



Submit