First of all, it is good to understand HTTP as a series of format conventions to be used over a common TCP connection. In principle it is a stateless protocol in which you basically send one text and get another one back.
In other words, HTTP does not process anything, but defines a format. It is the responsibility of the application that meets the request to process the data, and provide a consistent response to the protocol.
The Wikipedia even has a reasonable definition of HTTP, but I will try to highlight the most relevant points to the question in a way simpler soon after.
If you want to delve deeper into the specification after understanding the basics, follow the link to the W3 Consortium , which is responsible for defining and regulating the standard officially:
link
Format
An HTTP request is characteristic of this format:
| REQUISIÇÃO | RESPOSTA
------------+---------------------------------+--------------------------
CABEÇALHO | METODO CAMINHO PROTOCOLO/VERSAO | PROTOCOLO/VERSAO STATUS
| Cabeçalho 1: valor1 | Cabeçalho 1: valor1
| Cabeçalho 2: valor2 ... | Cabeçalho 2: valor2 ...
| Cabeçalho N: valorN | Cabeçalho N: valorN
linha vazia | |
CORPO | DADOS DO PEDIDO | DADOS DA RESPOSTA
-
MÉTODO
is GET
, POST
, PUT
, DELETE
, but is not limited to these. In addition, you can extend the protocol and define specific methods for the application you are using (and need to do the corresponding server-side method).
What are the HTTP request methods, and what is the difference between them?
What are the advantages of using the correct HTTP methods?
When should I use GET function and when should I use POST function?
-
CAMINHO
is the one that comes after the URL. Be just a /
slash or be a path to the resource as /blog/47894/como-fazer-amigos-e-influenciar-pessoas
. You can include a query string , as ?mode=json
at the end.
Important to note that anchors are not part of the way. At an address like /index.html?order=date#details
, the path is just /index.html?order=date
.
-
PROTOCOLO/VERSAO
is usually HTTP/1.1
, very rarely HTTP/1.0
, and now it works on HTTP/2.0
, which is already understood by some servers.
About this, there are some details here:
HTTP 1.1 vs HTTP 1.0
Exemplifying
When you access a web site as www.exemplo.com.br
, your browser resolves the address (turns www.exemplo.com.br
into an IP address, using DNS).
Next, it connects via TCP to the obtained IP, in principle on port 80 (which is the default of HTTP, being 443 the default of HTTPS). Once connected, it will send something like this, verbatim:
GET / HTTP/1.1
Host: www.exemplo.com.br
And that's all. Assuming there is a page in the requested address, you will get something like this back:
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Content-Encoding: UTF-8
Content-Length: 351
Connection: close
<html>...
Note that it's almost the same thing, except that in the answer instead of METODO CAMINHO PROTOCOLO
you have PROTOCOLO STATUS DESCRICAO_DO_STATUS
in the first line.
Methods
Once you understand the basic part of the protocol, let's see what changes if instead of a GET
we have a POST
. In the case of POST
we have some more information to send, and as described above, we use a blank line to separate the contents of the header:
POST /formulariodeinscricao.html HTTP/1.1
Host: www.example.com
nome=gato&senha=secret
Note that the key word here is POST
, the information for the first line, and in the request body (after the blank line) we have the values sent. In case I used the most common form of web forms, the format may vary depending on the case.
If it were a file upload with PUT
it might as well be:
PUT /upload.html HTTP/1.1
Host: www.example.com
DADOS_DO_ARQUIVO..........
Test Tool
There are some online tools that help you investigate and "debug" HTTP requests, one of them is this API, with several endpoints, which show a diversity of information, which helps a lot to test the sending part and the receipt of your application:
link
More references
What is a "stateless protocol," such as HTTP?
link