HTTP 415 Unsupported Media Type Error When Sending JSON Requests
When attempting to execute a REST service with a JSON request, a user encountered an HTTP 415 "Unsupported Media Type" error. Despite setting the request content type to "application/json; charset=utf8," it only worked when no JSON object was included in the request.
Using the google-gson-2.2.4 library and experimenting with different JSON libraries proved ineffective.
The following code snippet illustrates the implementation:
<code class="java">// Code snippet removed for brevity</code>
Examining the value of requestJson.toString(), which represented the JSON body:
<code class="json">{"type":"arl","action":"remove","domain":"staging","objects":"http://www.example.com"}</code>
Solution:
Curiously, removing "charset=utf8" from the "Content-Type" request property ("Content-Type", "application/json; charset=utf8") miraculously resolved the issue.
<code class="java">// Removed "charset=utf8" from "Content-Type" con.setRequestProperty("Content-Type", "application/json"); // Code snippet continues removed for brevity</code>
The exact reason for this alteration is still unknown, but it effectively allowed JSON requests to be sent and processed successfully.
The above is the detailed content of Why Does Removing \'charset=utf8\' from \'Content-Type\' Fix HTTP 415 \'Unsupported Media Type\' Error When Sending JSON Requests?. For more information, please follow other related articles on the PHP Chinese website!