When sending a JSON request to a REST API, you may encounter an "Unsupported Media Type" (HTTP 415) error. This typically indicates that the server is expecting a different content type for the request's body.
In the example provided, the problem stems from specifying "charset=utf8" in the Content-Type header. The server is likely configured to accept JSON requests with a content type of "application/json" without any additional charset specification.
To resolve the issue, modify the code to remove "charset=utf8" from the Content-Type header:
<code class="java">con.setRequestProperty("Content-Type", "application/json");</code>
This change ensures that the request is sent with a content type of "application/json" only, which is what the server is expecting.
The above is the detailed content of Why Do I Get an HTTP 415 Unsupported Media Type Error When Sending a JSON Request?. For more information, please follow other related articles on the PHP Chinese website!