Every request has a method, the verb that says what it does. GET asks for something and changes nothing. POST sends data to the server so it can create something new.
requests.post works like requests.get: the address first, then what to send. Pass json= and a dictionary, and requests turns it into JSON text, puts it in the request's body, and adds the header Content-Type: application/json so the server knows how to read it. For example, to send a note to a notes service:
note = {"text": "Call the supplier", "pinned": True}
reply = requests.post("https://notes.example/notes", json=note)
When a POST creates something, the server answers 201 Created, not 200, and the body usually describes what it made, with the id it gave it. Each POST changes the server: send two, and you create two things. Your script orders 3 mugs (product 1), then 10 pens (product 4):
POST /orders {"product_id": 1, "quantity": 3} → 201 {"order_id": 1001, "product_id": 1, "quantity": 3}
POST /orders {"product_id": 4, "quantity": 10} → order_id 1002
Status: 201 Order placed: 1001 Order placed: 1002
Your task: POST an order for 3 of product 1 as JSON to /orders, and print the status and the new order's id. Then POST a second order, 10 of product 4, and print its id.
Assignment
Follow the Coding Tutorial to place two orders with the supplier and read what it created.
Hintrequests.post takes the address, then json= and the dictionary to send. The answer's body is JSON: read it with response.json() and take its "order_id".
Free plan: 30 tutor messages. PRO is unlimited.
