DEV Community

Jason Stathopulos
Jason Stathopulos

Posted on

Basic Networking: HTTP Request & Response

The way HTTP which stands for HyperText Transfer Protocol is the basis for data communication on the internet, and the way it works is that a request from the client or browser asks the server for a file. The server gets the file if it is on the server. Then sends it back, to the client as a response, to render. If any files need to provide, then the browser will need to make an additional request.

Request

HTTP Request Uses methods sometimes called the verb such as GET and POST as well as the path/route the request is going to on the server, which is sometimes that is called the noun. The request uses headers for additional information such as content type.

The following is a sample HTTP request

GET(verb) /index.html(noun) HTTP/1.1
Host: localhost:1234
Headers: (Content-Type, etc.)
Enter fullscreen mode Exit fullscreen mode

One more thing, the web is inherently stateless meaning that the request is always new, when sending, to the server, and there is no connection between two or more requests which is the IP Address and Port to access the server. However, you could use cookies to keep track of the state for the server.

Response

After processing the request the server comes back with a response, status codes are sent to tell the client the results of what happened to the HTTP request on the server.

HTTP/version usually is 1.1 with a status number (200, 404) If it is okay or not found.
Content-type: type of content for HTML it is text/html

The content for example,

HTTP/1.1 200 OK or 404 NOT FOUND
Content-type: text/html
<!DOCTYPE html>
…
Enter fullscreen mode Exit fullscreen mode

Request Methods

A request method is important because it tells us what should we do with the request’s data. A server is not only able to retrieve data but also in charge of managing any changes to it as well. A request method combines with a URL.

Here are commonly used request methods, which are known as the ReSTful(Representational State Transfer) methods.

GET: fetch or read a resource. The URL contains all the necessary information the server needs to locate and return the resource without changing the server content. The GET request is simple. Every time you enter a URL in your browser or click on a link a GET request is made.

POST: create a new resource. POST requests usually carry a request body that specifies the data for the new resource. For the most part, when submitting forums after the user fills it in and hits submit, the browser will make a POST request to the server that includes the corresponding data in the request body.

PUT: replaces a resource on the server.
PATCH: is another type of update which updates an existing resource on the server two kinds of updates carry a request body with the updated data.

DELETE: delete a resource.

The other request types can also use with forums to send their request types to the server as well.

These also correspond to what we refer to as the CRUD actions. Create (POST), Read (GET), Update (PUT), and Delete (DELETE).

Response Codes

Response codes are the codes that come from the server to inform the client or browser if it was able to complete the request or not by using codes to describe what has happened on the server.

Here are the response codes which are Response codes are that are group by numbers, some of them you may have heard of before, like “200 OK” and “404 Not Found”.

1xx: Informational: These types of response codes not use. They were add in HTTP 1.1, rarely used.

2xx: Success: This tells the client that the request successfully process. The most common code is 200 OK. For a GET request, the server sends the resource in the message body.

3xx: Redirection: This requires the client to take additional action. The most common use-case is to jump to a different URL to fetch the resource.

4xx: Client Error: These codes use when the server thinks that the client is at fault, either by requesting an invalid resource or making a bad request. The most popular coded, in this class, is 404 Not Found, which everyone will identify with, 404 indicates that the resource is invalid and does not exist on the server.

5xx: Server Error: This class of codes are used to indicate a server failure while processing the request. The most commonly used error code is 500 Internal Server Error.

A full list of all response codes is available inside the HTTP official spec.

Top comments (0)