DEV Community

Cover image for Before sessions and cookies: HTTP request and response
Milan Zivkovic
Milan Zivkovic

Posted on

Before sessions and cookies: HTTP request and response

INTRO

Rails authentication is all about sessions and cookies. But before that I needed little refresh on HTTP requests and responses. It all sounded familiar but I wanted to have it all in one place as a reference when (not if) I need it.


Finding HTTP requests/responses

It's always good to see what are we talking about. Please see instructions hot to see HTTP requests/responses for Chrome browser:
*Navigate to page (anyone you like)
*Right click and hit 'Inspect'
*Select 'Network'
*Select any HTTP request on left side (panel named 'name') and headers will be displayed on right side


HTTP(Hypertext Transfer Protocol) request

HTTP request is a message that one computer sends to another using the HTTP protocol. It’s made by a client of ours API to our API.

Structure:

1. The Request line of the HTTP protocol (has three parts):
Example 1. GET /books HTTP/1.1
Example 2. POST /form.html HTTP/1.1

1.1 HTTP Method- indicate what kind of action our client wants to perform
GET- user wants to “read” a resource (example 1.)
POST- user wants to send us something (example 2.)

1.2 URI of request- refers to the address where the resource is located
Example 1. The user wants to “read”(GET) resources located at /books
Example 2. The user wants to send(POST) something (form.html), we will find this in the body of the HTTP request

1.3 HTTP protocol- there are several versions of HTTP (example HTTP/1.1)

2. The headers of the HTTP request are metadata that are sent in requests to provide information about the request.
Each header is specified with a name, then two points, and then followed by the value of that header. The header can be composed of several individual headers.

metadata in HTTP request- when we retrieve page the server sends with it various bits of information about the thing you are retrieving (metadata).
metadata in general- data that provide information about other data. Metadata summarizes basic information about data, making finding and working with particular instances of data easier.

3. The body of HTTP request place where we put additional information we want is going to send to the server. We can put whatever we want in the body of the request. The body represents (in many cases) content that we want to transmit.
GET requests do not use a body since it’s not sending many complex data when reading information. In the case of the POST method, we use the body of the request to send data to the server (sign in forms for example).


HTTP Response

Structure:

1. Status line
The request status is indicated in the response status line. If there are errors, if it was successful or if it is required some additional actions.
Read more about HTTP status code.

2. The response header
Is set of one or many headers (just like in HTTP request)

3. The body of the response
This part is optional, used when the server wants to transmit data to the user (for a web page it would be HTML (hypertext markup language) document)

sources:
HTTP headers, meta elements and language information
Anatomy of an HTTP request
about metadata
how to see HTTP headers

Top comments (0)