Home > Java > javaTutorial > Why Does Removing \'charset=utf8\' from \'Content-Type\' Fix HTTP 415 \'Unsupported Media Type\' Error When Sending JSON Requests?

Why Does Removing \'charset=utf8\' from \'Content-Type\' Fix HTTP 415 \'Unsupported Media Type\' Error When Sending JSON Requests?

Susan Sarandon
Release: 2024-11-03 04:05:03
Original
314 people have browsed it

Why Does Removing

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template