HTTP Protocol and REST API
Study the request-response model of HTTP, its methods and status codes, and the principles of REST architecture. Practice designing clear, resource-based APIs.
All topics (2 of 2)
Chapter 1. Operating systems
Chapter 2. Network technologies
HTTP (HyperText Transfer Protocol) is an application-layer protocol for exchanging data between a client (for example, a browser) and a server. It is based on the request-response model: the client sends a request and the server returns a response; a request consists of a method, an address (URL), headers and an optional body. Because HTTP is stateless, the server handles each request independently of the previous ones, and cookies or tokens are used to recognize a user. To protect the connection, HTTP runs over TLS, which is called HTTPS.
The most common methods are GET to read a resource, POST to create a new one, PUT to replace a resource completely, PATCH to change it partially and DELETE to remove it. GET is considered safe (it does not change the server state); GET, PUT and DELETE are idempotent, meaning that repeating the request has no additional effect on the server state, while POST is not idempotent. The status code in the response reports the result: 2xx means success (200 OK, 201 Created), 3xx redirection, 4xx a client error (404 Not Found) and 5xx a server error (500 Internal Server Error).
REST (Representational State Transfer) is an architectural style described by Roy Fielding in his doctoral dissertation in 2000. Its main idea is that everything (a user, an order, an article) is a resource with its own address, for example /users/42. An action on a resource is expressed by an HTTP method: GET /users/42 returns the user, while DELETE /users/42 removes it. A resource is usually transferred as JSON, and the client learns the data type from the Content-Type header.
REST relies on several constraints: separation of client and server, statelessness (each request carries all the information needed), cacheability, a uniform interface and a layered structure. In practice, the addresses of a good API use nouns rather than verbs (/orders, /orders/7), collections are named in the plural and the version is shown in the address (/v1/orders). Errors are returned with a suitable status code and a clear message, authentication usually relies on a token in the Authorization header, and the connection is protected with HTTPS.
Key takeaways
- HTTP is a stateless application-layer protocol built on the request-response model.
- Methods (
GET,POST,PUT,PATCH,DELETE) express the action on a resource, and status codes (2xx–5xx) express the result. GET,PUTandDELETEare idempotent, whilePOSTis not.- REST is an architectural style based on resources, their addresses and a uniform interface.
- A good API uses noun-based addresses, correct status codes, versioning and HTTPS.
Practice tasks
- In a terminal run
curl -i https://example.comand explain the status code of the response, theContent-Typeheader and two more headers. - Load any website with the Network tab of the browser DevTools open and write down the method, status code and size of at least three requests.
- Design a REST API for a books resource (
books): in a table write the HTTP method, the address and the expected status code for five actions: list, single book, create, update and delete.
Review questions
Test yourself: think first, then reveal the answer.
1Why is HTTP called a stateless protocol?Show answerHide answer
The server handles each request independently of previous ones and keeps no information about them; if a session is needed, the client sends a cookie or token every time.2What is the difference between the `PUT` and `POST` methods?Show answerHide answer
`PUT` completely replaces the resource at the given address and is idempotent; `POST` usually creates a new resource or triggers an action and, when repeated, may produce a new result each time.3How do the status codes `401` and `403` differ?Show answerHide answer
`401` means the client is not authenticated (its identity is unknown); `403` means the identity is known but access to the resource is not allowed.
0 of 2 topics completed
Sign in to mark