Home >>HTTP Tutorial >HTTP Requests

HTTP Requests

HTTP Requests

An HTTP client sends a request for HTTP to a server in the form of a request message including the following format:

HTTP Requests are messages sent by the client or user for the server to perform an action.

The message's first line contains the request message from the client to the server, the method applied to the resource, resource identifier and the protocol version.

Syntax

Request = Request-Line                
*(( general-header        
| request-header           
| entity-header ) 
CRLF) CRLF  
[ message-body ]  

Request Line

The Request-Line begins with a process token, followed by the Request-URI version of the protocol and ending with CRLF. The elements are separated using the characters SP.

Syntax

Request-Line   = Method SP Request-URI SP HTTP-Version CRLF  

Let's discuss about any part mentioned in the Request-Line.

Request Method

The request method indicates the method to be used on the resource specified by the Request-URI in query. The method is case-sensitive and should be stated in uppercase at all times. The table below lists all the methods allowed under HTTP/1.1.

Sr No Method and Description
1. GET
Using a given URI, the GET method is used to remember information from the given server. Applications using GET can only retrieve data and have no further impact on the data.
2. HEAD
Same as GET but it just moves the line of status and the section of headers.
3. POST
Using HTML types, for example, a POST request is used to send data to the server, customer information, file upload etc.
4. PUT
Replaces all of the target resource 's existing representations by the material uploaded.
5. DELETE
Removes all of the current target resource representations provided by URI.
6. OPTIONS
Describe aim resource communication choices.
7 TRACE
8. CONNECT
Sets up a tunnel identified by a given URI to the server.

Request-URI

The Request-URI is an Uniform Resource Identifier, defining the resource to send the request to. The most commonly used forms for specifying a URI follow:

Request-URI = "*" | absoluteURI | abs_path | authority
Sr No Method and Description
1. The asterisk * is used when an HTTP request does not apply to a particular resource, but to the server itself, and is only allowed when the method used does not necessarily apply to a resource. For example:
OPTIONS * HTTP/1.1
2. The absoluteURI is used when creating an HTTP request to a proxy. The proxy is requested from a valid cache to forward the request or service, and return the response. Take , for example:GET
http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1
3. The most popular type of Request-URI is that used on an origin server or gateway to define a resource. A client wishing to access a resource directly from the origin server, for example, will build a TCP connect to port 80 of the "www.w3.org" host and send the following lines:GET /pub/WWW/TheProject.html HTTP/1.1

Host: www.w3.org

 

Note that the absolute path cannot be empty; if none is present in the original URI, it MUST be given as "/" (the server root).

Request Header Fields

In a separate chapter, we will study General-header and Entity-header when we learn the fields of HTTP headers. For now, let's verify which fields are Request header.

  • The request-header fields allow the client to transfer additional information on the request to the server, and on the client itself. These fields serve as modifiers to requests.
  • Accept-Charset
  • Accept-Encoding
  • Accept-Language
  • Authorization
  • Expect
  • From
  • Host
  • If-Match
  • If-Modified-Since
  • If-None-Match
  • If-Range
  • If-Unmodified-Since
  • Max-Forwards
  • Proxy-Authorization
  • Range
  • Referer
  • TE
  • User-Agent

In case you want to write your own custom Client and Web Server, you can enter your custom fields.

Examples of Request Message

Now let's bring it all together to form an HTTP request to fetch hello.htm from the phptpoint.com operating web server.

GET /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

Here we do not send any request data to the server because we are downloading a server pure HTML file. Connection is a general-header, and the other headers are headers for requests. The example below shows how to send data from the form to the server using body of request message:

	POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
licenseID=string&content=string&/paramsXML=string

Here the provided URL /cgi-bin / process.cgi is used to process the passed data, and a response is returned accordingly. Here content-type informs the server that the data passed is a basic data web form and length would be the actual length of the data placed into the body of the message. The example below shows how to migrate plain XML to your web server:

POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://clearforest.com/">string</string>

No Sidebar ads