HTTP Fundamentals
Lesson opener
In one line: HTTP methods say what kind of operation you mean, and status codes say how it finished.
Why it matters: Northline's stock update was sent with GET and a body. The server ignored the body, the quantity never changed, and the caller logged "success" because the connection opened. The method was the defect.
Before this lesson: Introduction to APIs and enterprise integration.
What you will be able to do
- Choose GET, POST, PUT, PATCH, or DELETE for a named operation.
- Read a status code as success, caller error, or server error.
- Name two headers a serious request carries.
- Refuse to treat a transport success as a business success.
Prerequisites
Lesson 1. You do not need to run a server.
Methods, headers, and status codes
The method semantics below follow RFC 9110 (https://www.rfc-editor.org/rfc/rfc9110). They are properties of HTTP, not of one vendor.
| Method | Typical use in this course | Idempotent? |
|---|---|---|
| GET | Read a resource. No body is required. | Yes. Repeating it does not change the resource. |
| POST | Ask the server to process a new operation, often a create. | No. Repeating it can create a second record. |
| PUT | Replace the resource at that URL with the body you send. | Yes. Repeating the same body leaves the same result. |
| PATCH | Change only the fields you send. | Not guaranteed. Repeat it only if that API's contract says the patch is safe to repeat. |
| DELETE | Remove the resource at that URL. | Yes. Deleting it again still leaves it gone. |
Idempotent means one successful call and an identical repeat leave the resource in the same state. It does not mean "the server enjoys duplicates."
Headers used throughout this course:
Content-Type: application/jsonwhen the body is JSON.Authorizationwhen the caller must prove identity. The next lessons specify the scheme.Idempotency-Keyis a common convention some APIs document so a repeated POST is recognised. It is not required by HTTP. Use it only when that API's docs define it.
Status codes you will use. The class is the first digit.
- 200 OK. The request succeeded and a body usually follows.
- 201 Created. A new resource exists. The response should say where it is.
- 204 No Content. Success with an empty body. Do not parse a body that is not there.
- 400 Bad Request. The server could not understand the request.
- 401 Unauthorized. The caller did not authenticate. The name is historical. It means "not authenticated."
- 403 Forbidden. The caller is known and is not allowed to do this.
- 404 Not Found. The URL does not name a resource you may see. It can also hide a resource you must not know exists. Do not treat 404 as proof the business object was never real.
- 409 Conflict. The request clashes with the current state, such as a version someone else already changed.
- 422 Unprocessable Content. The syntax was readable and the values fail the server's rules. FastAPI uses this for validation errors. It is registered in the HTTP status code registry.
- 429 Too Many Requests. The caller exceeded a limit that API documents. RFC 6585 (
https://www.rfc-editor.org/rfc/rfc6585) defines this code. - 500, 502, 503. The server or a neighbour failed. The caller did not cause the body to be wrong.
An error is a response whose status is not in the 2xx class, plus a body that a person can read. A timeout is not a status code. It means no response arrived.
Teacher walkthrough: update on-hand quantity
Figures are illustrative. SKU FX-100 has 80 units in the warehouse.
- Read:
GET /items/FX-100. Expect 200 and a body withon_hand: 80. - Replace the whole item document:
PUT /items/FX-100with the full body. A second identical PUT still leaves 80. That is idempotent. - Change only the bin:
PATCH /items/FX-100with{ "bin": "A1" }. Do not assume a second PATCH is harmless unless the contract says so. - Receive a delivery as a new document:
POST /receipts. Two identical POSTs can create two receipts. That is why a receipt needs its own identity. - Remove a discontinued code:
DELETE /items/FX-100. A second DELETE still yields "gone," often 404 or 204, depending on the contract. Read that contract. Do not invent one status for every API.
Validation: the warehouse screen shows the same on_hand as the GET body after the write.
A second workplace case
A monitoring probe uses POST to "check health" because the author wanted a body. Each probe inserts a row. The health check is now a write. The fitting method is GET, which RFC 9110 classifies as safe: it should not change the resource.
Failure example: 200 with an error in the body
Symptom: The client treats every response as success. The body says "status": "error", "message": "unknown sku". Stock is not updated. The log says the call worked.
Diagnosis: The HTTP status was 200, so the client's "did the socket work" check passed. The business failure was hidden in the body. The root cause is a contract that reports failure as success.
Correction: Failures use a non-2xx status. The body may explain the error. The client branches on the status code first. A field named status inside a 200 body is not a substitute.
Validation: Call with SKU NO-SUCH. The status is 404, and the client does not write a stock movement.
Guided practice: pick the method and the status
Scenario: Northline must record one supplier payment of 1,500. Repeating the click must not pay twice. Figures are illustrative.
Hints:
- Is this a read or a new business event?
- What makes a repeat safe?
- What status should a missing supplier produce?
Complete solution:
Recording a payment is a new business event, so the first design is POST, and POST is not idempotent. The contract therefore requires an idempotency key that the server stores with the payment. The same key and the same body return the original result instead of a second payment. A supplier id that does not exist is 404, not 200. An authenticated caller who may not pay is 403, not 401. Validation: send the same key twice and count payments. The count stays 1.
Independent practice
Take one operation your team wants between two systems.
Acceptance criteria:
- You name one method and why it matches the table above.
- You say whether a repeat is safe, and what key or identity makes a POST repeat safe if you chose POST.
- You name the success status and one error status.
- You include
Content-Typewhen there is a body. - You do not describe a 200 response as the place to hide a failure.
Common mistakes
- Using GET with a body to perform a write.
- Retrying POST because "the network blinked," and creating a second order.
- Mapping every failure to 500, including bad input.
- Reading 401 and 403 as the same event. 401 means the caller is not authenticated. 403 means they are authenticated and not allowed.
Quick check
- Which methods in the table are idempotent by HTTP's own rules?
- What is the difference between 401 and 403?
- A client receives 204. What should it parse?
- Why is a second identical PUT of the same item body safe?
- Why is
Idempotency-Keynot something you may assume on every API?
Answers:
- GET, PUT, and DELETE. PATCH is not guaranteed. POST is not.
- 401: not authenticated. 403: authenticated and not permitted.
- Nothing. 204 means success with no body.
- PUT replaces the resource with that body. The same body leaves the same resource.
- It is a convention some APIs document. HTTP does not require it.
Lesson summary
Choose the method for the kind of operation, read the status before the body, and do not retry a non-idempotent call unless the contract gives you a key that makes the repeat the same operation.
Bridge to the next lesson
The bodies in these examples are JSON. The next lesson is how objects, arrays, and nested fields are shaped, and how you reject a payload before it touches the ERP.