Interview Prep/Python for DevOps/Data formats, HTTP & REST APIs

How do you send JSON, form data and files with `requests`?

BeginnerConceptData formats, HTTP & REST APIs
Answer
s.post(url, json={"name": "api", "replicas": 3}, timeout=10)      # sets Content-Type: application/json
s.post(url, data={"user": "ci", "token": t}, timeout=10)           # form-encoded
with open("report.html", "rb") as f:
    s.post(url, files={"file": ("report.html", f, "text/html")}, timeout=30)

A common mistake is data=json.dumps(payload) without the header, which the server receives as text/plain or form data and rejects with 415 or 400.

All Python for DevOps questions Roadmap: Python for automation

More on Data formats, HTTP & REST APIs