Home  >  Article  >  Java  >  Why Do I Get an HTTP 415 Unsupported Media Type Error When Sending a JSON Request?

Why Do I Get an HTTP 415 Unsupported Media Type Error When Sending a JSON Request?

Barbara Streisand
Barbara StreisandOriginal
2024-11-06 15:05:02824browse

Why Do I Get an HTTP 415 Unsupported Media Type Error When Sending a JSON Request?

HTTP 415 Unsupported Media Type with JSON Request

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!

Statement:
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