HTTP 415 Unsupported Media Type

Overview

The HTTP 415 Unsupported Media Type status code indicates that the server refuses to accept the request because the payload format is in an unsupported format. This response is typically sent when the server is unable to process the request payload due to its media type.

Purpose

The HTTP 415 response is used to inform the client that the media type of the request’s payload is not supported by the server or resource. It’s a way for the server to enforce specific content types for request data.

Usage

Client Behavior:

  1. Send Request with Unsupported Media Type: The client sends an HTTP request with a payload in a format not supported by the server.
  2. Receive Response: The client receives the HTTP 415 status code, indicating the media type of the payload is unsupported.

Server Behavior:

  1. Evaluate Media Type: The server checks the Content-Type header of the request to determine if the payload format is supported.
  2. Send Response: If the format is not supported, the server responds with a 415 Unsupported Media Type status code.

Scenarios

  • Strict Content Type Requirements: When a server or API requires a specific content type (e.g., application/json) for requests.
  • Invalid Content Submission: Client attempts to upload or post data in a format the server cannot process.

Sequence Diagram

Illustrating the process for an HTTP 415 response:

sequenceDiagram
    participant Client
    participant Server as Web Server

    Note over Client: Client sends a request with unsupported media type
    Client->>Server: POST /resource HTTP/1.1
    Client->>Server: Content-Type: text/plain
    Note over Server: Server checks and rejects unsupported type
    Server->>Client: HTTP/1.1 415 Unsupported Media Type

Curl Request and Response Example

Sending a request with an unsupported media type using Curl:

curl -i -X POST -H "Content-Type: text/plain" --data "plain text data" http://example.com/resource
# Expected response: HTTP/1.1 415 Unsupported Media Type

PHP cURL Request and Response Example

PHP script using cURL to handle a 415 Unsupported Media Type response:

<?php
$ch = curl_init('http://example.com/resource');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
curl_setopt($ch, CURLOPT_POSTFIELDS, 'plain text data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 415) {
    echo "Unsupported media type.";
}
curl_close($ch);
?>

Python Request and Response Example

Python script to send a POST request with an unsupported media type and handle a 415 response:

import requests
headers = {'Content-Type': 'text/plain'}
response = requests.post('http://example.com/resource', headers=headers, data='plain text data')
if response.status_code == 415:
    print("Unsupported media type")

Apache Configuration for HTTP 415 Unsupported Media Type

Configuring Apache to enforce specific media types for requests:

<VirtualHost *:80>
    ServerName example.com
    # Apache configurations to restrict or enforce specific media types
    # ...
</VirtualHost>

NGINX Configuration for HTTP 415 Unsupported Media Type

Setting up NGINX to respond to requests with unsupported media types:

server {
    listen 80;
    server_name example.com;
    location /resource {
        # NGINX directives to enforce specific media types
        # ...
    }
}

HTTP 414 Request-URI Too Long HTTP 416 Requested Range Not Satisfiable


 

Free Weekly

Newsletter

Join my weekly newsletter for the latest in tech! You'll get neat coding tricks, trend updates, career advice, SaaS reviews, crypto, bitcoin, and financial tips. All straight to your inbox, designed to keep you ahead.