Java
javaTutorial
Why Does Removing \'charset=utf8\' from \'Content-Type\' Fix HTTP 415 \'Unsupported Media Type\' Error When Sending JSON Requests?
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools




