REST API Fundamentals · Lesson 2 of 30
How HTTP Works: Requests, Responses and Headers
REST is built on HTTP, so understanding HTTP is understanding most of REST. HTTP is a plain-text conversation: the client sends a request, the server sends back one response. Once you can read both, debugging any API becomes far easier.
In this lesson
- The anatomy of a request and a response
- URLs: path parameters and query strings
- Headers that matter for APIs
- Reading real traffic with curl
A request and a response
POST /api/enrolments?notify=true HTTP/1.1 ← method, path + query, protocol
Host: api.learnfrenzy.com ← headers: metadata about the request
Content-Type: application/json
Authorization: Bearer eyJhbGciOi…
Accept: application/json
← blank line
{"courseId": "rest-api", "studentId": 42} ← body (optional)
HTTP/1.1 201 Created ← protocol, status code, reason
Content-Type: application/json; charset=utf-8
Location: /api/enrolments/1001
← blank line
{"id": 1001, "courseId": "rest-api", "studentId": 42, "status": "active"}
Parts of every HTTP message
- Start linerequest: method + target + version · response: version + status code + reason
- Headersname: value pairs – content type, auth, caching, length…
- Blank lineseparates headers from the body
- Bodyoptional data – JSON for most APIs
Seeing it for real
This server echoes back what it received, so you can see how each part of the URL and the headers arrive:
// server.js
import express from 'express';
const app = express();
app.use(express.json()); // parse JSON bodies
app.post('/api/courses/:courseId/reviews', (req, res) => {
res.status(201).json({
pathParams: req.params, // from the path: /courses/:courseId
query: req.query, // from ?key=value
contentType: req.get('Content-Type'),
acceptLanguage: req.get('Accept-Language') ?? null,
body: req.body,
});
});
app.listen(process.env.PORT ?? 3000);
$ curl -X POST 'http://localhost:3000/api/courses/rest-api/reviews?source=email&draft=false' -H 'Content-Type: application/json' -H 'Accept-Language: en-IN' -d '{"rating": 5, "comment": "Clear and practical"}'
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
{
"pathParams": {
"courseId": "rest-api"
},
"query": {
"source": "email",
"draft": "false"
},
"contentType": "application/json",
"acceptLanguage": "en-IN",
"body": {
"rating": 5,
"comment": "Clear and practical"
}
}- Path parameters identify which resource:
/courses/rest-api. - Query parameters refine the request – filters, sorting, pagination, options. They're always strings; convert and validate them on the server.
- Headers carry metadata: what format the body is in, what the client accepts, who it is.
- The body carries the data for POST, PUT and PATCH. GET requests shouldn't have one.
Headers you'll use constantly
| Header | Sent by | Meaning |
|---|---|---|
Content-Type | both | format of the body: application/json |
Accept | client | formats the client can handle |
Authorization | client | credentials: Bearer <token>, Basic … |
Location | server | URL of a newly created resource or a redirect target |
Cache-Control, ETag | server | whether and how the response can be cached |
Retry-After | server | when to try again (with 429 or 503) |
X-Request-Id | both | a correlation id for tracing a request through logs |
HTTP versions and HTTPS
HTTP/1.1, HTTP/2 and HTTP/3 carry the same methods, headers and status codes – they differ in how bytes travel (HTTP/2 multiplexes many requests on one connection; HTTP/3 runs over QUIC). As an API designer you can mostly ignore the version. HTTPS (HTTP over TLS) is not optional: it encrypts tokens, passwords and data in transit. Every production API must be HTTPS-only.
Practice
Try it yourself
Write a
GET /api/searchendpoint that returns theqandpagequery parameters, withpageconverted to a number (default 1).Show solution
// server.js import express from 'express'; const app = express(); app.get('/api/search', (req, res) => { const q = String(req.query.q ?? ''); const page = Number.parseInt(req.query.page ?? '1', 10) || 1; res.json({ q, page, pageType: typeof page }); }); app.listen(process.env.PORT ?? 3000);$ curl 'http://localhost:3000/api/search?q=rest+api&page=2' HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 { "q": "rest api", "page": 2, "pageType": "number" }
Quick check
What separates headers from the body?
A blank line.
Path parameter or query parameter for "which course"?
Path parameter: /courses/rest-api. Query parameters are for filters and options.
Which header tells the server the body is JSON?
Content-Type: application/json.
Summary
- HTTP messages have a start line, headers, a blank line and an optional body.
- Paths identify resources; query strings refine requests; headers carry metadata.
- Read real traffic with curl -i, and always use HTTPS in production.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.