Request Messages in HTTP

In this article, we will learn about Request Messages in HTTP.

Introduction

HTTP messages are of two different types,

  • HTTP request messages
  • HTTP response messages

Here, we’ll look at request messages.

HTTP Request Messages

Let’s first take a look at request messages. Here is an example of an ordinary HTTP message.

GET / HTTP/1.1
Host: www.codeintelligent.com
Connection: close
User-agent: Mozilla/5.0
Accept-language: en
Accept: text/html

Please be aware that,

  • Plain ASCII text is used for HTTP messages.
  • A carriage return and a line (\r\n) feed serve as the message’s two control characters at the end of each line.
  • Although HTTP messages may contain one or as many lines as necessary, this specific message comprises six lines.
  • The first line is known as the request line, while the subsequent lines are known as header lines.

The Structure of an HTTP Request Line

An HTTP header is placed after the HTTP request line. We’ll start by looking at the request line. The request line is divided into three sections,

  • Method
  • URL
  • Version

Let’s go through each one.

Structure of HTTP Request Line
Structure of HTTP Request Line

HTTP Methods

HTTP methods direct the server. There are several HTTP methods, however, we’ll focus on the most popular ones here: GET, POST, HEAD, PUT, or DELETE

  • The most typical method of requesting data is GET.
  • POST sends an object to the server.
    • When the client is unsure of where the new data might live, this approach is often employed. The server replies with the object’s location.
    • A message for a bulletin board, newsgroup, mailing list, command, online form, or anything to add to a database may all be included in the data submitted.
    • Technically, the POST method requests a page, but that relies on the data submitted.
  • HEAD is identical to GET, with the exception that it does not return the requested resource. Instead, only the HTTP headers are sent.
    • Without having to transfer the complete content, this is helpful for swiftly getting meta-information that has been put in response headers.
    • For testing and debugging, this is also beneficial.
  • PUT uploads a contained entity to a given URI. In other words, it places data in a certain place. If the URI points to an already existing resource, the new resource is used instead. The server creates the resource with that URI if it does not link to an existing resource.
  • DELETE removes an object from the specified URL.

Even though the majority of form submissions are sent via the POST method, the GET technique is sometimes used as well, with the form’s submissions attached to the URL, as in arguments like the following,

Get requests
Get requests

However, it is usually preferable to submit forms together with a POST request because,

  • An unlimited quantity of data may be delivered with a post request.
  • The URL does not display the form’s fields.

URL

Any HTTP method refers to this as the location.

Version

The request line also contains information about the HTTP version. HTTP/2 is the most recent version of HTTP.

The Structure of HTTP Header Lines

GET / HTTP/1.1
Host: www.codeintelligent.com
Connection: close
User-agent: Mozilla/5.0
Accept-language: en
Accept: text/html

An HTTP header is placed after the HTTP request line. There are several HTTP headers. The most significant ones will be covered in this article. However, you may learn more about each of them if you’re interested.

  • The “Host” for which the request is made is identified in the first header line.
  • The kind of HTTP “Connection” is specified in the second one. In the example, it is Non-persistent since the connection is shown as being closed.
  • The client is identified in the “user-agent” field. When the server has several web pages that are available for various hardware and browsers, this is advantageous.
  • The desired language is specified in the “Accept-language” header. If a web page in that language already exists, the server transmits it; otherwise, it sends the default page.
  • The kind of answer to accept is specified in the “Accept” header. Anything is acceptable, including HTML files, photos, and even music and video.

Gaurav Karwayun is the founder and editor in chief of CodeIntelligent. He has over 10+ Years of Experience in the software industry. He has experience working in both service and product-based companies. He has vast experience in all popular programming languages, DevOps, Cloud Computing, etc. Follow him on Twitter.

Leave a Comment