Setting the Content-Type Header for POST Requests with the Python Requests Library

Feb 1, 2024 ยท 2 min read

When making POST requests with the Python Requests library, it's important to set the Content-Type header to indicate the format of the data you are sending in the request body.

The Content-Type header allows the receiving server to understand how to parse the data you are sending. Some common content types are:

  • application/json - For sending JSON data
  • application/x-www-form-urlencoded - For sending form data
  • multipart/form-data - For sending files and form data
  • Here is an example of setting the Content-Type header to application/json when making a POST request with Requests:

    import requests
    url = ''
    data = {'name': 'John Doe'}
    headers = {'Content-Type': 'application/json'}
    response =, headers=headers, json=data)

    A few things to note:

  • We import the requests library to make the HTTP requests
  • We define a url variable with the API endpoint
  • The data variable contains a Python dict that will be JSON encoded
  • We set the Content-Type header to application/json to indicate the request body is JSON
  • We use the json parameter to automatically JSON encode the data
  • This sends a POST request with a JSON body
  • Some tips:

  • Always explicitly set the Content-Type. Don't rely on Requests to infer it.
  • For sending JSON, use the json parameter instead of data. This will encode it for you.
  • For form data, you may need to encode it first before passing it to data.
  • Setting the correct Content-Type ensures the server knows how to handle the data you send. It makes for more robust and reliable API integrations.

    Browse by language:

    Tired of getting blocked while scraping the web?

    ProxiesAPI handles headless browsers and rotates proxies for you.
    Get access to 1,000 free API credits, no credit card required!