POST a JSON Payload

Easy ⏱ 8 min 73% acceptance ★★★★★ 4.7
Write create_order(url, product_id, qty) sending a POST with json={'product_id': ..., 'qty': ...} — the json= kwarg sets the Content-Type header and serializes for you (never pass a pre-dumped string to data= for JSON APIs). Return the new order's id from the response body.

Examples

Example 1
Input
create_order(url, 42, 2)
Output
the 'id' field of the created order
Explanation

json= handles serialization and headers.

Constraints

  • Use json=, not data=.
  • Return response.json()["id"].

Topics

APIspost json

Companies

StripeRazorpayPayPal

Hints

Hint 1

requests.post(url, json=payload).

Hint 2

raise_for_status before reading the body.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.