HTTP

415 - Unsupported Media Type

Getting a 415 Unsupported Media Type means the server rejected your request because the Content-Type header doesn't match what the endpoint accepts—sending JSON when it expects XML, uploading a file format the server can't process, or missing the Content-Type header entirely. This client-side error (4xx) happens when servers validate request media types before processing. Most common when file uploads use wrong MIME types or API calls send data in unsupported formats, but also appears when Accept headers request formats the server doesn't support or Content-Type is missing or malformed.

#Common Causes

  • Frontend: Wrong Content-Type header (text/plain instead of application/json). File uploads with incorrect MIME type (image/jpeg vs image/jpg). Missing Content-Type header entirely. Accept header requests unsupported format (application/xml when server only does JSON). Browser auto-sets wrong Content-Type for FormData.
  • Backend: Server validation middleware rejects unsupported media types. File upload handler only accepts specific formats (e.g., only images, not PDFs). Content negotiation fails—server can't produce requested Accept format. Body parser configured for specific types only.
  • Infrastructure: Nginx/Apache blocks certain Content-Type values. WAF rules filter unsupported media types. Load balancer strips or modifies Content-Type headers. API gateway enforces stricter media type validation than backend.

Solutions

  1. 1Step 1: Diagnose - Check DevTools Network tab Request Headers—verify Content-Type value matches what server expects. Look for Accept header if making GET requests. Check if file uploads have correct MIME type.
  2. 2Step 2: Diagnose - Server logs show which media type was rejected and what's accepted. Review API documentation for supported Content-Type values. Check server configuration for media type restrictions.
  3. 3Step 3: Fix - Client-side: Set correct Content-Type header (application/json for JSON, multipart/form-data for forms). For file uploads, use file.type or detect MIME type correctly. Remove or fix Accept header if requesting unsupported format.
  4. 4Step 4: Fix - Server-side: Accept multiple media types (express.json() for JSON, express.raw() for binary). Return 415 with Accept header showing supported types. Configure body parsers for all needed formats. Update validation to be more permissive if appropriate.
  5. 5Step 5: Fix - Infrastructure: Review Nginx/Apache Content-Type filtering rules. Check WAF media type restrictions. Ensure load balancer passes Content-Type headers unchanged. Configure API gateway to accept same types as backend.

</>Code Examples

Fetch API: Set Correct Content-Type Headers
1// Client-side: Handle 415 by setting correct Content-Type
2async function sendData(data, contentType = 'application/json') {
3  const response = await fetch('/api/users', {
4  method: 'POST',
5  headers: {
6      'Content-Type': contentType, // Must match server expectations
7    },
8    body: contentType === 'application/json' 
9      ? JSON.stringify(data) 
10      : data,
11  });
12  
13  if (response.status === 415) {
14    const acceptedTypes = response.headers.get('Accept') || 
15                         response.headers.get('Content-Type');
16    throw new Error(`Unsupported media type. Server accepts: ${acceptedTypes}`);
17  }
18  
19  return response.json();
20}
21
22// For file uploads, detect MIME type correctly
23async function uploadFile(file) {
24  // Use file.type or detect MIME type
25  const contentType = file.type || 'application/octet-stream';
26  
27  const response = await fetch('/api/upload', {
28    method: 'POST',
29    headers: {
30      'Content-Type': contentType,
31    },
32    body: file,
33  });
34  
35  if (response.status === 415) {
36    const error = await response.json();
37    throw new Error(`File type ${contentType} not supported: ${error.message}`);
38  }
39  
40  return response.json();
41}
Express.js: Accept Multiple Media Types
1// Server-side: Configure body parsers for multiple media types
2const express = require('express');
3const multer = require('multer');
4const app = express();
5
6// Accept JSON
7app.use(express.json({ type: 'application/json' }));
8
9// Accept URL-encoded
10app.use(express.urlencoded({ extended: true, type: 'application/x-www-form-urlencoded' }));
11
12// Accept raw text
13app.use(express.text({ type: 'text/plain' }));
14
15// Accept XML (requires xml2js or similar)
16// app.use(express.text({ type: 'application/xml' }));
17
18// File uploads with multer
19const upload = multer({
20  fileFilter: (req, file, cb) => {
21    // Accept only specific file types
22    const allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'application/pdf'];
23    if (allowedTypes.includes(file.mimetype)) {
24      cb(null, true);
25    } else {
26      cb(new Error(`File type ${file.mimetype} not supported`), false);
27    }
28  },
29});
30
31app.post('/api/upload', upload.single('file'), (req, res) => {
32  res.json({ success: true, filename: req.file.originalname });
33});
34
35// Handle 415 errors
36app.use((error, req, res, next) => {
37  if (error.message.includes('not supported')) {
38    return res.status(415)
39      .set('Accept', 'image/jpeg, image/png, image/gif, application/pdf')
40      .json({
41        error: 'Unsupported Media Type',
42        message: error.message,
43        acceptedTypes: ['image/jpeg', 'image/png', 'image/gif', 'application/pdf'],
44      });
45  }
46  next(error);
47});
Nginx: Pass Content-Type Headers
1# Nginx: Ensure Content-Type headers reach backend
2server {
3    listen 80;
4    server_name api.example.com;
5    
6    location /api/ {
7        proxy_pass http://backend;
8        
9        # Pass Content-Type header to backend
10        proxy_set_header Content-Type $http_content_type;
11        proxy_set_header Accept $http_accept;
12        
13        # Don't modify Content-Type
14        proxy_pass_request_headers on;
15        
16        proxy_set_header Host $host;
17        proxy_set_header X-Real-IP $remote_addr;
18    }
19    
20    # Optional: Block specific Content-Types at Nginx level
21    # location /api/upload {
22    #     if ($http_content_type !~* "^image/(jpeg|png|gif)$") {
23    #         return 415 '{"error":"Unsupported Media Type","message":"Only JPEG, PNG, and GIF images are accepted"}';
24    #     }
25    #     proxy_pass http://backend;
26    # }
27}

Related Errors

Provider Information

This error code is specific to HTTP services. For more information, refer to the official HTTP documentation.

415 - Unsupported Media Type | HTTP Error Reference | Error Code Reference