HTTP: Requests, Responses, Methods, and Status Codes
HTTP (HyperText Transfer Protocol)
A web page consists of objects: files such as HTML, JPEG images, JavaScript, and CSS. A page containing one HTML file and five JPEG images has six objects.
Each object can be addressed by a URL. The server hostname and path identify where the object is stored. In http://www.someSchool.edu/someDepartment/picture.gif, the hostname is www.someSchool.edu and the path is /someDepartment/picture.gif.
HTTP specifications include RFC 1945, RFC 7230, and RFC 7540. In the HTTP versions described here, TCP supports communication: the client establishes a connection, sends request messages, and receives response messages from the server.
HTTP is stateless: the protocol does not automatically remember a client's earlier request. If a client requests the same object twice, receiving it once does not mean the server refuses to respond again. Applications can maintain state through additional mechanisms, which we will examine with cookies and sessions.
KNOWLEDGE QUIZ
Request Messages
The first line is the request line, followed by header lines. Each line ends with carriage return and line feed (CRLF), and an empty CRLF line terminates the headers.
GET /somedir/page.html HTTP/1.1\r\n
Host: www.someschool.edu\r\n
Connection: close\r\n
User-agent: Mozilla/5.0\r\n
Accept-language: fr\r\n
\r\n
[entity body]The request line has three fields:
-
A method, such as GET, POST, HEAD, PUT, or DELETE.
-
The request target identifying the object.
-
The HTTP version.
Host identifies the requested host. Connection: close asks the server to close the connection after the response rather than keep it persistent. User-agent identifies the client software, such as Mozilla/5.0, and can influence which representation a server sends. Accept-language expresses a preferred language; a server without that version may send a default.
The usual GET request has no entity body. A POST body can contain form input. HEAD is similar to GET but returns no response body, making it useful for examining metadata. PUT can upload an object's representation, and DELETE can remove an object.
Response Messages
A response consists of a status line, header lines, and an entity body when appropriate.
HTTP/1.1 200 OK\r\n
Connection: close\r\n
Date: Tue, 18 Aug 2015 15:44:04 GMT\r\n
Server: Apache/2.2.3 (CentOS)\r\n
Last-Modified: Tue, 18 Aug 2015 15:11:03 GMT\r\n
Content-Length: 6821\r\n
Content-Type: text/html\r\n
\r\n
(data data data data data ...)The status line contains the protocol version, status code, and reason phrase. Connection: close signals that the TCP connection will close after the message. Date identifies when the response was generated; Last-Modified describes the object's creation or last modification time. Content-Length specifies a length in bytes, and Content-Type identifies the representation's media type.
| Status | Meaning |
|---|---|
| 200 OK | Request succeeded |
| 204 No Content | No response content |
| 301 Moved Permanently | Permanent redirect to a new URL |
| 400 Bad Request | Request syntax could not be understood |
| 403 Forbidden | The request is refused |
| 404 Not Found | Resource not found |
| 500 Internal Server Error | Internal server error |
| 503 Service Unavailable | Temporarily unavailable due to overload or maintenance |
| 505 HTTP Version not supported | Unsupported HTTP version |
Construct a request and inspect its response
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
Separate the URL and interpret responses
Use Python's urllib.parse.urlsplit to print the hostname and path of http://www.someSchool.edu/someDepartment/picture.gif without accessing it. Then explain a GET response with 404 and a HEAD response with 200 and Content-Length: 500. State whether each case expects 500 body bytes.