An HTTP message contains several parts. We will define only the most important of them.
The URL of the message is the destination of the message. The request will contain the receiver's URL, and the response will contain the sender's.
As you might know, the URL can contain extra
parameters, known as a query string. This is used when the sender
wants to add extra data. For example, consider this URL:
http://myserver.com/greeting?name=Alex
.
This URL contains one parameter: name
with the value Alex
. It could not be
represented as part of the URL http://myserver.com/greeting
, so the sender chose to
add it at the end of it. You will see later that this is not
the only way that we can add
extra information into a message.
The HTTP method is the verb of the message. It identifies what kind of action the sender wants to perform with this message. The most common ones are GET and POST.
- GET: This asks the receiver about something, and the receiver usually sends this information back. The most common example is asking for a web page, where the receiver will respond with the HTML code of the requested page.
- POST: This means that the sender wants to perform an action that will update the data that the receiver is holding. For example, the sender can ask the receiver to update his profile name.
There are other methods, such as PUT, DELETE, or OPTION, but they are less used in web development, although they play a crucial role in REST APIs, which will be explained in Chapter 9, Building REST APIs.
The body part is usually present in response messages even though a request message can contain it too. The body of the message contains the content of the message itself; for example, if the user requested a web page, the body of the response would consist of the HTML code that represents this page.
Soon, we will discuss how the request can also contain a body, which is used to send extra information as part of the request, such as form parameters.
The body can contain text in any format; it can be an HTML text that represents a web page, plain text, the content of an image, JSON, and so on.
The headers on an HTTP message are the metadata that the receiver needs in order to understand the content of the message. There are a lot of headers, and you will see some of them in this book.
Headers consist of a map of key-value pairs. The following could be the headers of a request:
Accept: text/html Cookie: name=Richard
This request tells the receiver, which is a server, that it will accept text as HTML, which is the common way of representing a web page; and that it has a cookie named Richard.
The status code is present in responses. It identifies the status of the request with a numeric code so that browsers and other tools know how to react. For example, if we try to access a URL that does not exist, the server should reply with a status code 404. In this way, the browser knows what happened without even looking at the content of the response.
Common status codes are: