Skip to main content

How to Send JSON in a POST Request in Python with requests

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Most web APIs take their input as JSON in the body of a POST request. With the requests library, you pass a Python dictionary as json= and requests does the rest: it turns the dictionary into JSON and labels the body application/json. This guide sends a small order to a test service and shows what arrived, with real output from Python 3.15.2 and requests 2.34.2.

Syntax​

Syntax
response = requests.post(url, json=data, timeout=seconds)
PartWhat it means
urlThe address to send the request to
json=dataA dictionary or a list; requests turns it into JSON for the request's body
timeout=secondsHow long to wait for the server before giving up
responseWhat the server sent back

The requests quickstart: "you can also pass it directly using the json parameter (added in version 2.4.2) and it will be encoded automatically". It also warns: "the json parameter is ignored if either data or files is passed."

Prerequisites​

  • Python 3 with the requests package in your project, for example with uv add requests.
  • An internet connection: the example sends to httpbin.org, a public test service that answers with what it received.

Step 1: Write the Request​

The example sends an order for two coffees as a dictionary, then prints two things: the Content-Type header requests sent, and the JSON the server says it received.

post_json.py
import requests

order = {"item": "coffee", "quantity": 2}
response = requests.post("https://httpbin.org/post", json=order, timeout=10)

print("Sent as:", response.request.headers["Content-Type"])
print("Server received:", response.json()["json"])

The post_json.py script in a macOS-style code window: an order dictionary with item coffee and quantity 2, sent with requests.post to httpbin.org/post with json=order and timeout=10, then two prints of the Content-Type that was sent and the JSON the server received.

  • json=order: requests turns the dictionary into JSON text and adds the Content-Type: application/json header.
  • timeout=10: stop waiting after 10 seconds if the server doesn't answer.
  • response.request.headers: the headers requests really sent with the request.
  • response.json()["json"]: httpbin.org puts the JSON it received under the key json in its answer.

Step 2: Run It​

Terminal
uv run python post_json.py
Output
Sent as: application/json
Server received: {'item': 'coffee', 'quantity': 2}

The script's output in a terminal: Sent as: application/json, and Server received: item coffee, quantity 2.

The body was labelled application/json, and the server read the same item and the same number, 2, still a number.

Common Mistakes​

MistakeWhat happensDo this instead
data=order instead of json=orderrequests sends a form, application/x-www-form-urlencoded: the server's json was None, and the form held '2' as textjson=order
json=json.dumps(order)The JSON text is encoded a second time, so the server received one string, '{"item": "coffee", "quantity": 2}', not an objectPass the dictionary itself
Passing json= together with data=The quickstart: the json parameter "is ignored if either data or files is passed"Use one of them

Troubleshooting​

SymptomLikely causeFix
ModuleNotFoundError: No module named 'requests'requests isn't installed where the script runsuv add requests in the project
The server says the body is missing or isn't JSONThe data went as a formCheck response.request.headers["Content-Type"]; it should be application/json

FAQ​

What exactly did requests send?​

response.request.body shows it. For this example it was b'{"item": "coffee", "quantity": 2}', the dictionary written as JSON.

Can I send a list instead of a dictionary?​

Yes. json=[1, 2, 3] arrived at httpbin.org as the list [1, 2, 3].

Conclusion​

requests.post(url, json=order, timeout=10) sent the dictionary as JSON with Content-Type: application/json, and the server received {'item': 'coffee', 'quantity': 2} with the number intact. data= would have sent a form instead, and json.dumps first would have sent a single string.

Additional Resources​