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:
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
How to Update Each Element in a List in Java 8 Using Stream API
Intellij Incorrectly Saying No Beans of Type Found for Autowired Repository
How to Create Unique Column With Jpa
How to Test Code Dependent on Environment Variables Using Junit
Remove Trailing Comma from Comma-Separated String
Reverse a String Without Affecting Special Characters
Java 8 Convert Utc Time to Edt/Est So That Date Remains the Same
Requestmethod Post Not Supported || Spring Login
Java - How to Find Students With Their Highest Marks Writing a Method in a Student Class
Spring Data JPA - Consider Defining a Bean Named 'Entitymanagerfactory' in Your Configuration
Spring Boot - How to Get Running Port and Ip Address
Check If a Given Time Lies Between Two Times Regardless of Date
Arraylist - How to Check If an Index Exists
How to Remove Trailing Zeros from a Double
How to Sort Integer Digits in Ascending Order Without Strings or Arrays