Fixing "Content-Type incorrect" Errors with Python Requests

Feb 3, 2024 ยท 2 min read

When using the Python Requests library to make HTTP requests, you may occasionally see an error like:

requests.exceptions.InvalidHeader: Value for header Content-Type is invalid

This happens when Requests is unable to determine the correct Content-Type header to send with the request body.

Why Content-Type Matters

The Content-Type header tells the server what format the data is being sent in - like JSON, form data, etc. This allows the server to correctly parse and process the request body. Without the proper Content-Type, the server may reject the request or parse the data incorrectly.

Common Causes

There are a few common reasons you may see invalid Content-Type errors:

  • Sending JSON without setting Content-Type: By default, Requests won't set a Content-Type when sending JSON. So you need to manually set it to application/json.
  • Incorrect format for form-encoded data: Form data needs to be a dictionary that Requests can encode. An invalid format can cause incorrect Content-Type headers.
  • Forgetting to encode file objects: When sending files, you need to wrap them with open(file, 'rb') so Requests can properly handle encoding.
  • Solutions

    Here are some tips to avoid invalid Content-Type errors:

  • Explicitly set Content-Type for JSON: headers = {'Content-Type': 'application/json'}
  • Use correct format for form-encoded data: The data parameter should be a dictionary.
  • Encode file objects correctly: files = {'file': open('report.xls', 'rb')}
  • Taking a bit of extra care to ensure the request body and headers match the expected Content-Type can save you some frustrating errors! The Requests documentation contains more great examples too.

    Browse by tags:

    Browse by language:

    The easiest way to do Web Scraping

    Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


    Try ProxiesAPI for free

    curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

    <!doctype html>
    <html>
    <head>
        <title>Example Domain</title>
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    ...

    X

    Don't leave just yet!

    Enter your email below to claim your free API key: