The HTTP protocol can be likened to a series of questions and answers, referred to respectively as HTTP Requests and HTTP Responses.
The HTTP Request
Once the connection to the server has been opened, the HTTP client transmits a request as follows:
- An opening line
- A number of header lines (optional)
- A blank line
- A message body (optional)
- The name of the method (POST, GET, PUT, DELETE or HEAD). Method names are always uppercase.
- The path to the required server resource. The path is the part of the URL after the host name, also called the request URI (a URI is like a URL, but more general).
- The HTTP version being used. The HTTP version is always in uppercase and takes the form HTTP/x.x.
GET /path/to/file/index.html HTTP/1.0
This tells the server that it is receiving an HTTP request of type GET, using HTTP version 1.0, and the server resource we are using, including it's local path, is /path/to/file/index.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, separated by a colon.
For example, a header line sent from IE9 might look like:
User-Agent: Mozilla /5.0 (Compatible MSIE 9.0;Windows NT 6.1;WOW64; Trident/5.0)
Another example of a common request header is the Accept: header, which states what sort of information will be acceptable in the server's response.
For example
Accept: text/plain, text/html
This header informs the server that the sending application can accept only plain text or HTML responses.
If the HTTP request includes a message body, the header lines describe the content of the body.
For example
Content-Type: text/plain
This header informs the server that the message body contains text
.
In an HTTP request, the message body is where user-entered data or uploaded files are sent to the server.The HTTP Response
The server issues an HTTP response in answer to an HTTP request.
The first line of the HTTP response i known as the status line. This consists of three parts separated by spaces:
- the HTTP version. This is in the same format as for the HTTP request.
- a three digit integer, called the HTTP response status code, that gives the result of the request, and
- a short message known as the reason phrase describing the status code.
HTTP/1.0 200 OK
or
HTTP/1.0 404 Not Found
The response status code and the reason phrase are intended as computer- and human-readable versions of the same message. The reason phrase may vary from server to server.
The first digit of the status code identifies the general category of response. Some commonly encountered HTTP Response Status codes are:
Status Code | Explanation |
---|---|
1** indicates an informational message only | |
100 - Continue | The client should send the remainder of the request |
101 - Switching Protocols | The server will switch protocols to those defined following header |
2** indicates success of some kind | |
200 - OK | The request succeeded |
204 - No Content | The document contains no data |
3** redirects the client to another URL | |
301 - Moved Permanently | The resource has permanently moved to a different URI |
4** indicates an error on the client's part | |
401 - Not Authorized | The request needs user authorization |
403 - Forbidden | The server has refused to fulfil 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 |
5** indicates an error on the server's part | |
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.
For example:
Server: Apache/2.4.5
Last Modified: Wed, 20 Nov 2013 13:33:59 GMT
No comments :
Post a Comment