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
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
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
Hope this helps explain why sometimes Requests may send JSON even if you expect it to send form-urlencoded data!