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/
Spring boot: Getting 415 unsupported Media type
Solved! I accidentally had two setters defined with different data types.
415 unsupported media type angular spring boot POST PUT http methods
Hi guys finally I found the problem
I removed @JsonManagedReference
in a class related to one of the classes related to mine ,I really recommend not to work with it try to use @JsonIgnore
instead
415 Unsupported Media Type - POST json to OData service in lightswitch 2012
It looks like this issue has to do with the difference between the Content-Type
and Accept
headers. In HTTP, Content-Type
is used in request and response payloads to convey the media type of the current payload. Accept
is used in request payloads to say what media types the server may use in the response payload.
So, having a Content-Type
in a request without a body (like your GET request) has no meaning. When you do a POST request, you are sending a message body, so the Content-Type
does matter.
If a server is not able to process the Content-Type
of the request, it will return a 415 HTTP error. (If a server is not able to satisfy any of the media types in the request Accept
header, it will return a 406 error.)
In OData v3, the media type "application/json" is interpreted to mean the new JSON format ("JSON light"). If the server does not support reading JSON light, it will throw a 415 error when it sees that the incoming request is JSON light. In your payload, your request body is verbose JSON, not JSON light, so the server should be able to process your request. It just doesn't because it sees the JSON light content type.
You could fix this in one of two ways:
- Make the Content-Type "application/json;odata=verbose" in your POST request, or
Include the DataServiceVersion header in the request and set it be less than v3. For example:
DataServiceVersion: 2.0;
(Option 2 assumes that you aren't using any v3 features in your request payload.)
"Content type 'application/json;charset=UTF-8' not supported" in Spring Rest application
I was able to solve it by removing @JsonManagedReference.
Related Topics
How to Post Form Data With Spring Resttemplate
Spring JPA Selecting Specific Columns
Mockito Not Mocking Private Variables Using @Spy
Error Creating Bean With Name and Singleton Bean Creation Not Allowed
How to Query Multiple Hash Keys in Dynamodb
Java.Lang.Illegalargumentexception: No Converter Found for Return Value of Type
Find Value for a Key from a Dynamic Json Using Java
Re-Prompt User After Invalid Input in Java
Is There a Simpler Way to Check Multiple Values Against One Value in an If-Statement
Hibernate Error Executing Ddl Via Jdbc Statement
How to Pass a List and a Separate String Using @Requestbody in Springboot
The Import Android.Support Cannot Be Resolved
How to Get the Position of a Element in a Queue
How to Join Results of Multiple Tables in Spring JPA Repository
Spring Boot - Exception Encountered During Context Initialization. How to Know the Reason of Error
How to Skip the First Line of a CSV in Java
How to Convert a One-Dimensional Array to Two-Dimensional Array in Java