Зміст курсу
Spring Boot Backend
Spring Boot Backend
How HTTP Works
You might have wondered how your requests are processed and how pages with data are loaded depending on the URL you enter in your browser. The HTTP protocol helps us with this process.
What is meant by a client? In the context of HTTP, a client refers to a program or device that initiates requests to a server to retrieve data or resources. Most commonly, the client is a web browser (such as Chrome, Firefox, or Safari) that you use to access websites.
The server's response can be in various formats. You might receive an HTML page that your browser will display, or you may send and receive information in JSON or XML formats (which we will cover later).
Example
Here’s a real-life example: Imagine you are ordering food at a restaurant. You (the client) place an order with the waiter (sending a request), and the waiter forwards your order to the kitchen (the server). Then, the kitchen prepares your meal and the waiter brings it back to you (returning a response).
In this example, you are the web browser, the waiter is the HTTP protocol, and the kitchen is the server that processes requests and sends responses.
HTTP Methods
HTTP methods define what actions should be performed on resources on the server. The main methods include:
GET
Used to retrieve data from the server. For example, when you open a web page, your browser sends a GET request to load its content.
Imagine you go to a library and ask the librarian (the server) to find a book (the resource). The librarian goes to the shelf, retrieves the book, and returns it to you.
When you open a web page, your browser sends a GET
request to "ask" the server to provide the page, just as you would ask for a book at the library.
POST
Sends data to the server to create a new resource. This method is often used to submit form data, such as registration details or comments.
For example, you submit an application for a passport, and the relevant department creates a new record based on your information.
Similarly, when you submit data through a form on a website (such as registration details), a POST
request sends that data to the server to create a new resource.
PUT
Used to update existing resources on the server. This method allows you to modify data on the server based on the provided information.
It is similar to updating an existing document. In the web context, a PUT
request is used to replace or update an existing resource on the server. For example, you can update information about your user profile.
DELETE
Used to remove resources from the server. For example, this method can be used to delete a user account or a forum post.
Imagine a company decides to remove an outdated contract from its archive. An employee locates the document and deletes it from the database.
PATCH
Used for partial updates to a resource on the server. Unlike a PUT
request, which replaces the entire resource, PATCH
is used when you need to change only part of the resource.
For example, if you want to update a user's email address in their profile, you can use a PATCH
request to modify just the email field.
HEAD
Used to request only the headers of a resource, without retrieving its content.
In a web context, a HEAD
request is used to obtain metadata about a resource. For example, it can be used to check if a page has been modified since the last visit or to verify the existence of a file on the server before downloading it.
How Does this Work in Practice?
Request
When you enter a URL (codefinity.com
) into the search bar, you expect to receive a page with information in response.
In order for a page to be displayed, your browser (the client) sends a request to the server in the following format:
This image illustrates an example of an HTTP request that a client (such as a browser) sends to a server. The request uses the GET
method, which tells the server that the client wants to retrieve data (in this case, the homepage /
). The protocol version is specified as HTTP/1.1
.
The headers provide additional information: Host
indicates that the request is directed to the server at codefinity.com
, User-Agent
gives details about the type of client (browser) making the request, and Accept-Language
specifies the preferred language for the response — English. Since this is a GET
request, there is no body, as there’s no need to send data to the server.
Response
After receiving the request, the server sends an HTTP response, which also consists of several parts:
This image shows an example of an HTTP response that a server sends back to a client. The first line specifies the protocol version HTTP/1.1
, confirming that it's the same version used in the request. Following that is the status code 200 OK
, indicating that the request was successful and the server is returning the requested data.
The headers provide additional details about the content being sent. For instance, Content-Type: text/html; charset=UTF-8
specifies that the content is an HTML document encoded in UTF-8. Content-Length: 3056
indicates the size of the content in bytes, while Server: Apache/2.4.41 (Ubuntu)
gives information about the web server that processed the request.
The body of the response contains the actual HTML code for the page being delivered to the client, which in this case is a simple HTML document with a header and a welcome message.
Дякуємо за ваш відгук!