For code that acts as a HTTP client or server.
Hypertext Transfer Protocol (HTTP) is an application level network protocol used for the transfer of content on the World Wide Web.
HTTP uses a client-request/server-response model. It is a stateless protocol, which means it does not require the server to retain information or status about each user for the duration of multiple requests.
The request is sent with an HTTP method:
- GET - used to retrieve data, the request body should be ignored.
- POST - used to send data to the server, the body should hold the data
These are all the methods supported by older browsers, but the HTTP 1.1 specification includes a few more: HEAD, PUT, DELETE, TRACE, OPTIONS, CONNECT and PATCH
The response is returned with a status code:
- 1xx are informational
- 2xx indicates success, most pages will have a 200 status
- 3xx are used for redirections
- 4xx are used for errors with the request, the commonest being 404 for a page not found
- 5xx are used for server errors
Both the request and response are made up of a header and an optional body.
The header contains a list of key-value pairs, separated using new lines and colons; for instance, a request may have headers like this:
User-Agent: browser name or client application
Note that in the example the request is telling the server that the response can be sent with the body compressed with either gzip or deflate encoding.
The request needs a body if it's sending additional data to the server; for instance, when sending information entered to a form.
The response headers will include information telling the client how to deal with the response data - whether they can cache the data (and for how long), for instance.
The response body will have the requested data, such as the HTML of a web page or image data.