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

MethodPurposeHas Body
GETRetrieve resourceNo
POSTCreate resourceYes
PUTUpdate resourceYes
DELETERemove resourceNo

Status Codes

RangeCategoryExamples
1xxInformational100 Continue
2xxSuccess200 OK, 201 Created
3xxRedirection301 Moved, 304 Not Modified
4xxClient Error400 Bad Request, 404 Not Found
5xxServer Error500 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.