Sending JSON vs Form Data in Python Requests

Feb 3, 2024 ยท 2 min read

When making HTTP requests in Python using the requests module, you can send request bodies in different formats like JSON or form-urlencoded data. However, sometimes the request body is sent incorrectly or unexpectedly as JSON even if you want to send form data.

The Difference Between JSON and Form Data

JSON (JavaScript Object Notation) is a lightweight data format that looks like JavaScript objects. For example:

{"key1": "value1", "key2": "value2"} 

Form-urlencoded data is essentially key-value pairs encoded in a URL format, like:

key1=value1&key2=value2

Servers can handle both JSON and form data, but they need to be parsed differently on the backend.

Why Requests May Send JSON Instead of Form Data

By default, if you pass a dict to the data parameter in Requests, it will get JSON encoded automatically:

import requests

data = {'key1': 'value1', 'key2': 'value2'}

r = requests.post(url, json=data) # Sends JSON

So if you want to send form data instead, you need to explicitly use the data parameter instead of json:

import requests

data = {'key1': 'value1', 'key2': 'value2'}  

r = requests.post(url, data=data) # Sends form data

The key thing to remember is that Requests handles JSON encoding for you, but not form encoding. So use json for JSON and data with a dict for form data.

Hope this helps explain why sometimes Requests may send JSON even if you expect it to send form-urlencoded data!

Browse by tags:

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!