shaare.it

Invalid Content-Type Header

7 Dec 2025

1 min read

Invalid Content-Type header

$ python -c "import requests; requests.post('https://api.example.com/items', headers={'Content-Type':'text/plain'}, data='{"x":1}')"
Traceback (most recent call last):
  ...
requests.exceptions.HTTPError: 415 Client Error: Unsupported Media Type

Why this happens

APIs expect specific media types (e.g., application/json). Mismatched Content-Type breaks parsing.

Fix

Set Content-Type to match the body. Prefer json= for JSON; let Requests set the header automatically.

Wrong code

import requests
requests.post('https://api.example.com/items', headers={'Content-Type':'text/plain'}, data='{"x":1}')

Fixed code

import requests
requests.post('https://api.example.com/items', json={'x': 1}, timeout=10)