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.
http.Post takes three things: the address, the content type of the body, and a reader for the body. Turn a map into JSON bytes with json.Marshal, wrap them with bytes.NewReader, and pass "application/json": http.Post puts it in the header Content-Type: application/json so the server knows how to read the body. For example, to send a note to a notes service:
note := map[string]any{"text": "Call the supplier", "pinned": true}
body, _ := json.Marshal(note)
reply, _ := http.Post("https://notes.example/notes", "application/json", bytes.NewReader(body))
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 program 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.
Hinthttp.Post takes the address, "application/json" and bytes.NewReader(body), where body comes from json.Marshal. The answer's body is JSON: decode it into a map[string]any and take its "order_id".
Free plan: 30 tutor messages. PRO is unlimited.
