error http

HTTP 415 Unsupported Media Type

Understanding HTTP 415 Unsupported Media Type - the server refuses the request because the payload format is not supported.

What It Means

HTTP 415 Unsupported Media Type indicates that the server refuses to accept the request because the payload format is in an unsupported format. The problem might be with the Content-Type or Content-Encoding header, or by directly inspecting the data.

Common Causes

  • Missing Content-Type header when sending JSON
  • Sending application/x-www-form-urlencoded when the server expects application/json
  • Uploading a file type not accepted by the server (e.g., .exe when only images are allowed)
  • Sending XML when the API only accepts JSON
  • Mismatched Content-Type with actual body content
  • Using text/plain instead of application/json

How to Fix

Set the correct Content-Type header

// Bad: Missing Content-Type
fetch('/api/users', {
  method: 'POST',
  body: JSON.stringify({ name: 'John' })
});

// Good: Include correct Content-Type
fetch('/api/users', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ name: 'John' })
});

// For form data - let the browser set the Content-Type
const formData = new FormData();
formData.append('file', fileInput.files[0]);
fetch('/api/upload', {
  method: 'POST',
  body: formData
  // Do NOT set Content-Type manually for FormData
});

curl

# Bad: defaults to application/x-www-form-urlencoded
curl -X POST https://api.example.com/users -d '{"name":"John"}'

# Good: explicitly set Content-Type
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d '{"name":"John"}'

Server-side validation (Express.js)

app.post('/api/data', (req, res) => {
  const contentType = req.headers['content-type'];

  if (!contentType || !contentType.includes('application/json')) {
    return res.status(415).json({
      error: 'Unsupported Media Type',
      message: 'Content-Type must be application/json',
      received: contentType || 'none'
    });
  }

  // Process the request
});

File upload validation

const multer = require('multer');

const upload = multer({
  fileFilter: (req, file, cb) => {
    const allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
    if (allowedTypes.includes(file.mimetype)) {
      cb(null, true);
    } else {
      cb(new Error('Unsupported file type'), false);
    }
  }
});

app.post('/api/upload', upload.single('image'), (req, res) => {
  if (!req.file) {
    return res.status(415).json({
      error: 'Unsupported Media Type',
      message: 'Only JPEG, PNG, and GIF images are accepted'
    });
  }
  res.json({ url: `/uploads/${req.file.filename}` });
});
  • HTTP 400 - Bad Request: The request itself is malformed, not just the wrong format.
  • HTTP 413 - Payload Too Large: The correct format but exceeds size limits.