Blog 2: HTTP Request Methods and Response Codes

Blog 2: HTTP Request Methods and Response Codes

Hey everyone, welcome to my second blog. Today, we will discuss about HTTP methods and response codes. To understand HTTP request better, go through my first blog once Understanding HTTP Request and Response.

Okay so let's get started. We know that client interacts with server in the form of HTTP requests. Every request will have HTTP methods which will define the type of actions that need to be performed on the server. Any software program performs 4 basic operation, CREATE, READ, UPDATEand DELETE which in short called CRUD . To perform these operations, we have majorly 4 different methods. POST, GET, PUT and DELETE. Let's see one by one.

  • GET : This method is used to fetch data from server. Whenever we want to access data present on server, we use GET method. It can be used only to retrieve data and nothing else and hence, it is a safe method. ( We will see what a safe method means)
  • POST : This method is used to create data on server. It writes new records to server and changes the database. Hence, it is not a safe method.
  • PUT : This method is used to update data or content. POST and PUT might look similar as both changes the content but PUT is used to update the existing resource and POST adds a new one.
  • DELETE : This method removes the content from the server. So we should be careful while using it.

Let's discuss now what is safe methods. A method that doesn't cause any change in the database or the server are safe methods. Since GET is only used to retrieve data, it is a safe method. PUT, POST and DELETE can change the content on server as these are used to create, update and remove data respectively. Hence, these are unsafe methods. Another important term with methods is i.e. Idempotent. Idempotent methods are the one which has same effect if called multiple times. For example, let's say we have to fetch a record whose name is Bhumika, so even if we try to fetch multiple times via GET request that won't affect the database. Similarly, if we are trying to update or delete data whose name is Bhumika, then, once the data is deleted, then multiple request for same won't affect the database. But this not same in case of POST method. Whenever we do POST, new record gets inserted in database. Hence, POST method id not Idempotent. There are many other HTTP methods, you can refer the MDN Doc to know more.

HTTP Response Codes

We know that server sends back the response for every request that it receives. That response contains a response code which tells the client about the status of the request, whether the request was successfully completed or it got failed. There are various codes that define status of the response. These are categorized in 5 groups as mentioned below.

RANGE    DESCRIPTION
100 - 199    Informational codes
200 - 299    Success codes
300 - 399    Redirection codes
400 - 499    Client error codes
500 - 599    Server error code

100 Continue : This code indicates that client should continue the request or should ignore the response that was already sent.

200 OK : It is the success code which indicates the request is completed without any issues. That means, we got whatever we had requested from the server.

204 No Content : In this case, the request is successful but there was no need to return content. That means, the request from client itself didn't require any content rather just meta data information.

400 Bad Request : This code is returned when there is some error in the syntax of the request raised by client which server cannot understand.

403 Forbidden : This code indicates that the client does not have access right to the content and hence the request is not successful.

404 Not Found : This code means that the requested content is not found on the server. It can also mean that the URL is not recognized or correct.

500 Internal Server Error : It means that server has encountered an unexpected condition which can not be handled by server.

503 Service Unavailable : This code means that the server is not available because it is down or overloaded.

We have discussed few of the most common ones. I will suggest you to read the MDN docs which has explained each group in detail. I hope you find this blog helpful. Please feel free to share your feedback and suggestions. Thank you so much for reading! See you in the next blog, till then, take care and keep learning!