/ HTTP Status Codes

415 Unsupported Media Type

The 415 Unsupported Media Type HTTP status code is a server response indicating that the server cannot process the request due to an unsupported media type in the request’s payload. This status code is part of the HTTP/1.1 standard and is used when the server is unable to process the request because the media type of the submitted data is not supported or understood by the server.

When Does a Server Return 415 Unsupported Media Type?

A server returns the 415 Unsupported Media Type status code when the client sends a request with a payload in a format that the server cannot process. This usually occurs when the Content-Type header in the request is set to a media type that the server does not support.

For example, if a server is designed to process JSON data, and the client sends a request with an XML payload, the server may return a 415 Unsupported Media Type status code, indicating that it cannot process the XML data.

Example: Request and Response

Let’s look at an example to better understand the 415 Unsupported Media Type status code. In this example, the client sends a POST request to create a new resource with an unsupported media type.

Request

POST /api/resources HTTP/1.1
Host: example.com
Content-Type: application/xml
Content-Length: 56

<resource>
  <name>Example Resource</name>
</resource>

In the request above, the client attempts to create a new resource by sending a POST request with an XML payload. The Content-Type header is set to application/xml.

Response

HTTP/1.1 415 Unsupported Media Type
Content-Type: application/json
Content-Length: 92

{
  "status": 415,
  "message": "Unsupported Media Type. Please use 'application/json' as Content-Type."
}

In the response, the server returns a 415 Unsupported Media Type status code because it cannot process the XML data in the request. The server also provides a helpful error message, suggesting that the client should use application/json as the Content-Type.

Handling 415 Unsupported Media Type in Your Application

When developing an application, it’s important to handle the 415 Unsupported Media Type status code properly to ensure a smooth user experience. Here are some steps you can take to handle this status code:

  1. Validate the Content-Type header: Before processing the request, check the Content-Type header to ensure that it’s set to a supported media type.
  2. Return a helpful error message: When returning a 415 Unsupported Media Type status code, include a helpful error message in the response body to inform the client of the correct media type to use.
  3. Document supported media types: In your API documentation, clearly specify the supported media types for each endpoint to prevent confusion and reduce the likelihood of clients using unsupported media types.
  4. Handle the error on the client-side: On the client-side, implement error handling for the 415 Unsupported Media Type status code to gracefully handle the error and inform the user of the issue.

Summary

The 415 Unsupported Media Type HTTP status code is an important server response that indicates the server cannot process the request due to an unsupported media type in the request’s payload. By understanding this status code and implementing proper error handling in your applications, you can provide a better user experience and prevent potential issues related to unsupported media types.

Was this helpful?

Thanks for your feedback!