Describing the Format of HTTP Requests
- An HTTP client sends an HTTP request to a server
-
An HTTP request maintains the following format:
- A request line
-
Zero or more header lines
- Including general, request, and entity fields
- An empty line
- Optional message body
Describing the Format of HTTP Responses
- First, a server receives a HTTP request message
- Then, that server responds with an HTTP response message
- HTTP responses maintain a similar format as HTTP requests
-
They maintain the following format:
- A request line
- Zero or more header lines
- An empty line
- Optional message body
Components of HTTP Requests and Responses
-
Request Line:
Contains:- A request method
- A URL of the desired resource
General Header:
General info about a request or responseEntity Header:
Info about the resource of a requestRequest Header:
Information about the clientResponse Header:
Information about the serverMessage Body:
Data meant for the client or server recipient- An HTTP request has a request header
- An HTTP response has a response header
- A typical request looks like the following:
Defining HTTP Request Methods
-
GET Method
- This method is given a server URI
- This method is used to retrieve information from this server
- Specifically, this method should only retrieve data
- It should not have any other effect on the data
-
HEAD Method
- This method is also given a server URI
- It is also used to retrieve information from this server
- However, it returns the status line and header section only
-
POST Method
- This method is used to send date to a server
- This method represents an addition of data
-
For example, this data could include:
- Customer information
- File uploads
- Data from HTML files
-
PUT Method
- This method is used to replace data from a server
- Specifically, it replaces this data with a given resource
- The target resource is given by a URI
- This method represents a replacement of data
-
DELETE Method
- This method is used to remove data from a server
- Specifically, the target resource is given by a URI
- The target resource is removed from the server
-
CONNECT Method
- This method is used to establish a tunnel to the server
- Specifically, the server is identified by a given URI
-
OPTIONS Method
- This method is used to describe the options of communication for the target resource
-
TRACE Method
- This method is used to perform a message loop-back test to a target resource
Examples of HTTP Requests
- GET Method
GET /test.html HTTP/1.1
Host: www.test101.com
Accept: image/gif, image/jpeg, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0
Content-Length: 35
bookId=12345&author=Mike+Lane
- HEAD Method
HEAD /test.html HTTP/1.1
Host: www.test101.com
Accept: image/gif, image/jpeg, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0
Content-Length: 35
bookId=12345&author=Mike+Lane
- POST Method
POST /bin/login HTTP/1.1
Host: www.test101.com
Accept: image/gif, image/jpeg, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0
Content-Length: 35
User=Peter+Lee&pw=123456&action=login
- PUT Method
PUT /new.html HTTP/1.1
Host: www.test101.com
Accept: image/gif, image/jpeg, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0
Content-Length: 31
Content-type: text/html
<p>New File</p>
- DELETE Method
DELETE /file.html HTTP/1.1
- CONNECT Method
CONNECT server.example.com:80 HTTP/1.1
Host: server.example.com:80
Proxy-Authorization: basic aGVsbG86d29ybGQ=
- OPTIONS Method
OPTIONS /resources/post-here/ HTTP/1.1
Host: bar.other
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Origin: http://foo.example
Access-Control-Request-Method: POST
Access-Control-Request-Headers: X-PINGOTHER, Content-Type
- TRACE Method
TRACE /index.html
Defining HTTP Response Codes
- HTTP response codes indicate the status of an HTTP request
-
These responses are grouped into five classes:
-
Informational Responses:
100-199 codes- The request was received and the process is continuing
-
Successful Responses:
200-299 codes- The action was successfully received and understood
-
Redirectional Responses:
300-399 codes- The action must be continued to complete the request
-
Client Errors:
400-499 codes- The request contains incorrect syntax
-
Server Errors:
500-599 codes- The server failed to fulfill any request
-
References
Previous
Next