HTTP - REQUEST AND RESPONSE
MESSAGES
Assistant Lecturer: Daniel Mwaibata
Department of Computer Science and Information System
St. Joseph University in Tanzania
For any web developer you should have come across the term HTTP and HTTP
requests.
Knowing about these terms will make you understand how the client-
server architecture works and how the client and server communicate.
The concept of HTTP.
What is HTTP?
It stands for hypertext transfer protocol. Using this protocol the client sends a
request to the server and based on the request the server and the web browser
respond to the client.
In a nutshell, we can say that it’s the base using which one computer (the client)
communicates with another (the server).
HTTP requests are made by a client to request an action on a resource identified
by a URI (Uniform Resource Identifier). There are several types of HTTP requests,
each designed for specific actions:
1|Page
HTTP - REQUEST AND RESPONSE
MESSAGES
Assistant Lecturer: Daniel Mwaibata
Department of Computer Science and Information System
St. Joseph University in Tanzania
Making HTTP Request
Once the connection is established using the HTTP protocol between client and
server, the client then sends a request in the form of binary data to the server
asking to access specific files or information from the server.
What’s actually inside the request
Every HTTP request contains three elements which are
i)Request Line,
ii)Request Header, and
iii)Body of Request(optional)
What is actually inside HTTP request
2|Page
HTTP - REQUEST AND RESPONSE
MESSAGES
Assistant Lecturer: Daniel Mwaibata
Department of Computer Science and Information System
St. Joseph University in Tanzania
Request line
1. It specifies the method, which tells the server what to do with the
information or resource.
2. It contains the URL of the request which is used to find the resource on the
server.
3. It also specifies HTTP protocol version being used (Ex. HTTP/ 1.0 or
HTTP/1.1)
Request Header
It consists of 0 or more headers.
The headers are used to pass more information about the request so that using the
request headers the server knows how to deal with the information the client is
demanding.
I.e What should be the language of content to be displayed, what should be the
content-type that client demands.
3|Page
HTTP - REQUEST AND RESPONSE
MESSAGES
Assistant Lecturer: Daniel Mwaibata
Department of Computer Science and Information System
St. Joseph University in Tanzania
Request Body
This is an optional part of the HTTP request which is used to send additional data
to the server.
Why it’s optional?
Consider a post request that we make to submit a form of data –
The data from the form should be present in the request body of the HTTP request
so that the server can access the data and save it for future use.
4|Page
HTTP - REQUEST AND RESPONSE
MESSAGES
Assistant Lecturer: Daniel Mwaibata
Department of Computer Science and Information System
St. Joseph University in Tanzania
On the other hand, if we see a get request by the client: -
As here we are requesting a page from the server and not passing any data to the
server, thus request body is inappropriate.
The Hypertext Transfer Protocol (HTTP) is designed to enable communications
between clients and servers.
HTTP works as a request-response protocol between a client and server.
Example: A client (browser) sends an HTTP request to the server; then the server
returns a response to the client. The response contains status information about
the request and may also contain the requested content.
HTTP Methods
GET
POST
PUT
HEAD
5|Page
HTTP - REQUEST AND RESPONSE
MESSAGES
Assistant Lecturer: Daniel Mwaibata
Department of Computer Science and Information System
St. Joseph University in Tanzania
DELETE
PATCH
OPTIONS
CONNECT
TRACE
The two most common HTTP methods are: GET and POST.
The GET Method
GET is used to request data from a specified resource.
Note that the query string (name/value pairs) is sent in the URL of a GET request:
/test/demo_form.php?name1=value1&name2=value2
Some notes on GET requests:
GET requests can be cached
GET requests remain in the browser history
GET requests can be bookmarked
GET requests should never be used when dealing with sensitive data
GET requests have length restrictions
GET requests are only used to request data (not modify)
The POST Method
POST is used to send data to a server to create/update a resource.
The data sent to the server with POST is stored in the request body of the HTTP
request:
POST/test/demo_form.php HTTP/1.1
Host: w3schools.com
name1=value1&name2=value2
Some notes on POST requests:
POST requests are never cached
POST requests do not remain in the browser history
6|Page
HTTP - REQUEST AND RESPONSE
MESSAGES
Assistant Lecturer: Daniel Mwaibata
Department of Computer Science and Information System
St. Joseph University in Tanzania
POST requests cannot be bookmarked
POST requests have no restrictions on data length
Compare GET vs. POST
The following table compares the two HTTP methods: GET and POST.
GET POST
BACK Harmless Data will be re-
button/Reload submitted (the
browser should
alert the user
that the data are
about to be re-
submitted)
Bookmarked Can be bookmarked Cannot be
bookmarked
Cached Can be cached Not cached
Encoding type application/x-www-form-urlencoded application/x-
www-form-
urlencoded or
multipart/form-
data. Use
multipart
encoding for
binary data
7|Page
HTTP - REQUEST AND RESPONSE
MESSAGES
Assistant Lecturer: Daniel Mwaibata
Department of Computer Science and Information System
St. Joseph University in Tanzania
History Parameters remain in browser history Parameters are
not saved in
browser history
Restrictions on Yes, when sending data, the GET No restrictions
data length method adds the data to the URL; and
the length of a URL is limited (maximum
URL length is 2048 characters)
Restrictions on Only ASCII characters allowed No restrictions.
data type Binary data is
also allowed
Security GET is less secure compared to POST POST is a little
because data sent is part of the URL safer than GET
because the
Never use GET when sending passwords parameters are
or other sensitive information! not stored in
browser history
or in web server
logs
Visibility Data is visible to everyone in the URL Data is not
displayed in the
URL
8|Page
HTTP - REQUEST AND RESPONSE
MESSAGES
Assistant Lecturer: Daniel Mwaibata
Department of Computer Science and Information System
St. Joseph University in Tanzania
PUT Method
PUT is used to send data to a server to create/update a resource.
The difference between POST and PUT is that PUT requests are idempotent. That
is, calling the same PUT request multiple times will always produce the same
result. In contrast, calling a POST request repeatedly have side effects of creating
the same resource multiple times.
HEAD Method
HEAD is almost identical to GET, but without the response body.
In other words, if GET /users returns a list of users, then HEAD /users will make
the same request but will not return the list of users.
A HEAD request is useful for checking what a GET request will return before
actually making a GET request - a HEAD request can read the Content-Length
header to check the size of the file, without actually downloading the file.
DELETE Method
The DELETE method deletes the specified resource.
PATCH Method
The PATCH method is used to apply partial modifications to a resource.
OPTIONS Method
The OPTIONS method describes the communication options for the target
resource.
9|Page
HTTP - REQUEST AND RESPONSE
MESSAGES
Assistant Lecturer: Daniel Mwaibata
Department of Computer Science and Information System
St. Joseph University in Tanzania
CONNECT Method
The CONNECT method is used to start two-way communications (a tunnel) with
the requested resource.
TRACE Method
The TRACE method is used to perform a message loop-back test that tests the
path for the target resource (useful for debugging purposes).
HTTP Response
The response from the server with the target to provide the client with the desired
resources is HTTP Response.
let’s look inside the Response from the server:-
Status line
HTTP/1.1 302 Found This is how the response header's status line looks. It
contains the HTTP protocol version, status code, and Reason phrase (known as
status text).
Response Header
10 | P a g e
HTTP - REQUEST AND RESPONSE
MESSAGES
Assistant Lecturer: Daniel Mwaibata
Department of Computer Science and Information System
St. Joseph University in Tanzania
There can be one or more response header lines and they are used to pass
additional information to the client from the server.
Response Body
The response body contains the resource demanded by the client. If the request is
unsuccessful then the response body contains the reason for the error, it may also
contain the steps to be done by the client to complete the request successfully.
11 | P a g e
HTTP - REQUEST AND RESPONSE
MESSAGES
Assistant Lecturer: Daniel Mwaibata
Department of Computer Science and Information System
St. Joseph University in Tanzania
Conclusion
We till now know all about the HTTP request and response and what content is
present in both.
These 2 terms are very crucial to be aware of if you are developing web sites
and web apps and have to make requests to the server.
12 | P a g e