CertGrid CertGrid
Hands-on Lab·Python Automation for IT

Python JSON API Requests and Responses

Sending data is where the errors get interesting: `data=` and `json=` do completely different things to the same dict, a rejected request has an explanation you have to go and fetch, and `json=` refuses five types that appear in real payloads - datetimes, Decimals, UUIDs, Paths and sets. This guide fixes each, and explains why money goes on the wire as a string.

APIs and Cloud Guide 26 of 39 Intermediate

Written against the versions above. `json=` has been in `requests` since 2.4.2. `json.dumps(default=...)` is standard library and unchanged. The lab's fixture echoes back whatever it receives, which makes the round trip visible.

Everything on this page runs on the control node. Any machine with Python 3 will do.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSControl node - every script in this path runs here2 Core4 GB50 GB

Before you start

  1. Posting JSON

  2. json= against data=

  3. A 400, and reading what the server said about it

  4. What json= refuses to serialise

  5. So convert them, deliberately

  6. Decimal, and why str rather than float

  7. The whole thing as one function

Official sources