Lesson 7 of 15
HTTP Request & Response
HTTP Request & Response
HTTP (HyperText Transfer Protocol) is the foundation of data exchange on the web. It follows a request-response model where a client sends a request and a server returns a response.
HTTP Request Structure
GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0
Accept: text/html
A request has:
- Request line: method, path, HTTP version
- Headers: key-value pairs
- Body (optional, for POST/PUT)
HTTP Response Structure
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 13
Hello, World!
Common Methods
| Method | Purpose | Has Body |
|---|---|---|
| GET | Retrieve resource | No |
| POST | Create resource | Yes |
| PUT | Update resource | Yes |
| DELETE | Remove resource | No |
Status Codes
| Range | Category | Examples |
|---|---|---|
| 1xx | Informational | 100 Continue |
| 2xx | Success | 200 OK, 201 Created |
| 3xx | Redirection | 301 Moved, 304 Not Modified |
| 4xx | Client Error | 400 Bad Request, 404 Not Found |
| 5xx | Server Error | 500 Internal Server Error |
Your Task
Implement parseHTTPRequest(raw) that parses a raw HTTP request string into an object with: method, path, version, headers (object), and body.
Also implement buildHTTPResponse(status, statusText, headers, body) that builds a raw HTTP response string.
Node.js loading...
Loading...
Click "Run" to execute your code.