- Introducing HTTP
- The HTTP Request and Response
- HTML Forms
- Summary
The HTTP Request and Response
The HTTP protocol can be likened to a conversation based on a series of questions and answers, which we refer to respectively as HTTP requests and HTTP responses.
The contents of HTTP requests and responses are easy to read and understand, being near to plain English in their syntax.
This section examines the structure of these requests and responses, along with a few examples of the sorts of data they may contain.
The HTTP Request
After opening a connection to the intended server, the HTTP client transmits a request in the following format:
- An opening line
- Optionally, a number of header lines
- A blank line
- Optionally, a message body
The opening line is generally split into three parts; the name of the method, the path to the required server resource, and the HTTP version being used. A typical opening line might read:
GET /sams/testpage.html HTTP/1.0
In this line we are telling the server that we are sending an HTTP request of type GET (explained more fully in the next section), we are sending this using HTTP version 1.0, and the server resource we require (including its local path) is
/sams/testpage.html.
Header lines are used to send information about the request, or about the data being sent in the message body. One parameter and value pair is sent per line, the parameter and value being separated by a colon. Here's an example:
User-Agent: [name of program sending request]
For instance, Internet Explorer v5.5 offers something like the following:
User-agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
A further example of a common request header is the Accept: header, which states what sort(s) of information will be found acceptable as a response from the server:
Accept: text/plain, text/html
By issuing the header in the preceding example, the request is informing the server that the sending application can accept either plain text or HTML responses (that is, it is not equipped to deal with, say, an audio or video file).
The HTTP Response
In answer to such a request, the server typically issues an HTTP response, the first line of which is often referred to as the status line. In that line the server echoes the HTTP version and gives a response status code (which is a three-digit integer) and a short message known as a reason phrase. Here's an example HTTP response:
HTTP/1.0 200 OK
The response status code and reason phrase are essentially intended as machine-and human-readable versions of the same message, though the reason phrase may actually vary a little from server to server. Table 3.1 lists some examples of common status codes and reason phrases. The first digit of the status code usually gives some clue about the nature of the message:
- 1**—Information
- 2**—Success
- 3**—Redirected
- 4**—Client error
- 5**—Server error
Table 3.1. Some Commonly Encountered HTTP Response Status Codes
Status Code |
Explanation |
200 - OK |
The request succeeded. |
204 - No Content |
The document contains no data. |
301 - Moved Permanently |
The resource has permanently moved to a different URI. |
401 - Not Authorized |
The request needs user authentication. |
403 - Forbidden |
The server has refused to fulfill the request. |
404 - Not Found |
The requested resource does not exist on the server. |
408 - Request Timeout |
The client failed to send a request in the time allowed by the server. |
500 - Server Error |
Due to a malfunctioning script, server configuration error or similar. |
The response may also contain header lines each containing a header and value pair similar to those of the HTTP request but generally containing information about the server and/or the resource being returned:
Server: Apache/1.3.22 Last-Modified: Fri, 24 Dec 1999 13:33:59 GMT