Http 415 Unsupported Media type error with JSON
A HTTP 415 Unsupported Media Type error means that the server is unable to process the request because the request entity has a media type that the server does not support. In the context of a JSON request, this means that the server is unable to process
A HTTP 415 Unsupported Media Type error means that the server is unable to process the request because the request entity has a media type that the server does not support. In the context of a JSON request, this means that the server is unable to process the JSON payload because it does not recognize or accept the specified media type.
There are a few potential reasons why you might see a HTTP 415 Unsupported Media Type error when sending a JSON request:
- The
Content-Typeheader is not set correctly: TheContent-Typeheader specifies the media type of the request entity. For a JSON request, theContent-Typeheader should be set toapplication/json. If theContent-Typeheader is missing or set incorrectly, the server may not recognize the media type and return a 415 error. - The server does not support the media type specified in the
Content-Typeheader: Even if theContent-Typeheader is set correctly, the server may still return a 415 error if it is not configured to handle that specific media type. - Strict media type validation: Some servers or frameworks require exact matches, including charset parameters (e.g.,
application/json; charset=utf-8). If the header is too generic or contains unsupported parameters, strict servers may reject it. Note that malformed or invalid JSON typically triggers a 400 Bad Request, not a 415.
To fix a HTTP 415 Unsupported Media Type error, ensure the Content-Type header matches what the server expects and verify that your JSON payload is syntactically valid. Below are examples of how to correctly set the header in common tools and frameworks:
cURL
curl -X POST https://api.example.com/data \
-H "Content-Type: application/json" \
-d '{"key": "value"}'Java (Spring Boot)
@PostMapping(value = "/data", consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> handleJson(@RequestBody MyDto payload) {
// Process payload
return ResponseEntity.ok("Success");
}If the issue persists, verify the server's configuration to ensure it explicitly supports application/json and check server logs for specific media type rejection messages.
I hope this helps! Let me know if you have any questions.