Webmaster in a Nutshell

Previous Chapter 17 Next
 

17.2 Client Requests

Client requests are broken up into three sections. The first line of a message always contains an HTTP command called a method, a URI that identifies the file or resource the client is querying, and the HTTP version number. The following lines of a client request contain header information, which provides information about the client and the data entity it is sending the server. The third part of a client request is the entity body, the data being sent to the server.

URI (Uniform Resource Identifier) is just a general term for all valid formats of addressing schemes supported on the World Wide Web. The one in common use now is URL (Uniform Resource Locator) addressing scheme. See \ Chapter 1, Introduction, for more information on URLs.

Methods

A method is an HTTP command that begins the first line of a client request. The method tells the server the purpose of the client request. There are three main methods defined for HTTP: GET, HEAD, and POST. Other methods are also defined, but are not as widely supported by servers as GET, HEAD, and POST (although the other methods will be used more often in the future, not less). Methods are case sensitive, so a "GET" is different than a "get."

The GET method

GET is a request for information located at a specified URI on the server. It is the most commonly used method by browsers to retrieve documents for viewing. The result of a GET request can be generated in many different ways. It could be a file accessible by the server, the output of a program or CGI script, the output from a hardware device, etc.

When a client uses the GET method in its request, the server responds with a status line, headers, and the requested data. If the server cannot process the request due to an error or lack of authorization, the server usually sends a textual explanation in the data portion of the response.

The entity-body portion of a GET request is always empty. GET is basically used to say "Give me this file." The file or program that the client requests is usually identified by its full path name on the server.

Here is an example of a successful GET request to retrieve a file. The client sends:

GET /index.html HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/2.02Gold (WinNT; I)
Host: www.ora.com
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*

The server responds with:

HTTP/1.0 200 Document follows
Date: Fri, 20 Sep 1996 08:17:58 GMT
Server: NCSA/1.5.2
Last-modified: Mon, 17 Jun 1996 21:53:08 GMT
Content-type: text/html
Content-length: 2482
(body of document here)

The GET method is also used to send input to programs like CGI through form tags. Since GET requests have empty entity-bodies, the input data is appended to the URL in the GET line of the request. When a <form> tag specifies the method="GET" attribute value, key-value pairs representing the input from the form are appended to the URL following a question mark (?). Pairs are separated by an ampersand (&). For example:

GET /cgi-bin/birthday.pl?month=august&date=24 HTTP/1.0

causes the server to send the birthday.pl CGI program the month and date values specified in a form on the client. The input data at the end of the URL is encoded to CGI specifications. For literal use of special characters, the client uses hexadecimal notation. The character encoding is described in Chapter 9, CGI Overview.

The GET method can also supply extra-path information in the same manner. This is achieved by adding the extra path after the URL, i.e., /cgi-bin/ display.pl/cgi/cgi_doc.txt. The server gauges where the program's name ends (display.pl). Everything after that is read as the extra path.

The HEAD method

The HEAD method is functionally like GET except that the server will not send anything in the data portion of the reply. The HEAD method requests only the header information on a file or resource. The header information from a HEAD request should be the same as that from a GET request.

This method is used when the client wants to find out information about the document and not retrieve it. Many applications exist for the HEAD method. For example, the client may desire the following information:

  • Modification time of a document, useful for cache-related queries

  • The size of a document, useful for page layout, estimating arrival time, or determining whether to request a smaller version of the document

  • The type of the document, to allow the client to examine only documents of a certain type

  • The type of server, to allow customized server queries

It is important to note that most of the header information provided by a server is optional and may not be given by all servers. A good design for Web clients is to allow flexibility in the server response and take default actions when desired header information is not given by the server.

The following is an example HTTP transaction using the HEAD request. The client sends:

HEAD /index.html HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/2.02Gold (WinNT; I)
Host: www.ora.com
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*

The server responds with:

HTTP/1.0 200 Document follows
Date: Fri, 20 Sep 1996 08:17:58 GMT
Server: NCSA/1.5.2
Last-modified: Mon, 17 Jun 1996 21:53:08 GMT
Content-type: text/html
Content-length: 2482

(No entity body is sent in response to a HEAD request.)

The POST method

The POST method allows data to be sent to the server in a client request. The data is directed to a data handling program that the server has access to (e.g., a CGI script). The POST method can be used for many applications. For example, it can be used to provide input for:

  • Network services, such as newsgroup postings

  • Command-line interface programs

  • Annotation of documents on the server

  • Database operations

The data sent to the server is in the entity-body section of the client's request. After the server processes the POST request and headers, it passes the entity-body to the program specified by the URI. The encoding scheme most commonly used with POST is URL-encoding, which allows form data to be translated into a list of variables and values for CGI processing. Chapter 9, CGI Overview provides details on CGI and URL-encoded data.

Here is a quick example of a client request using the POST method to send birthdate data from a form:

POST /cgi-bin/birthday.pl HTTP/1.0
User-Agent: Mozilla/2.02Gold (WinNT; I)
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
Host: www.ora.com
Content-type: application/x-www-form-urlencoded
Content-length: 20
month=august&date=24

Other methods

The following methods are also defined, although not as frequentlly used.

LINK

Requests that header information is associated with a document on the server.

UNLINK

Requests dissociation of header information from a document on the server.

PUT

Requests that the entity-body of the request be stored at the specified URI.

DELETE

Requests the removal of data at a URI on the server.

OPTIONS

Requests information about communications options available on the server. The request URI can be substituted with an asterisk (*) to indicate the server as a whole.

TRACE

Requests the request entity body be returned intact. Used for debugging.


Previous Home Next
HTTP Basics Book Index Server Responses