Most times you submit a form on a website, you are making a request. Every time you go to a new page, you are making a request. These requests are shoved into a header and sent to another piece of code and then you get the page you wanted. You’ve probably seen the HTTP requests at some point in your code, especially if you work with APIs.
Just so you have a quick reference for what these HTTP requests are and when they are used, here are some of the most common ones.
This is one of the most commonly used HTTP request methods because it's how you take user input and send data to a server. When a user creates a new account that means you are going to perform a POST request. If somebody buys something on your website you are using the POST method.
The transfer of information from the front-end to the back-end typically has something do with the POST method. It's mostly used with HTML forms, but you'll use them a lot with APIs in the form of attributes. You have to be careful with POST requests. Calling them multiple times could make multiple updates to your server that you don't want.
The GET method is another commonly used HTTP request method. You use this to request data from a specific resource. That resource could be a database, blob storage, or some other type of data resource. This is how you load pages for users based on the permissions they have or some other condition.
This method is only used to request data, not change it or add new data. If you need to show a user some of their information, you can use this method but you have to be careful. Don't use this method with sensitive information like social security numbers or bank account information. That information could be intercepted by a ne'er-do-well and then you're in a lot of trouble.
The PUT request is very similar to the POST request. It makes a new resource or it updates an existing one. The main difference is what happens when you call the method. Unlike the POST method, calling PUT multiple times will have the same affect on your data resource each time. If you call a POST request multiple times, there is a chance you could end up with duplicate data or incorrect data.
If a resource, like blob storage, already has an instance of the data you are trying to create then you simply update it. With a POST request, you might get a duplicate instead of an updated version. PUT requests are better for when you know more about how the resource is setup and how it should be managed.
The HEAD request is similar to the GET request except it doesn't return the response body. That means you won't be able to see the detailed result of the request. All you will have access to are the headers that get returned. That can be useful if you are trying to decide whether you want to make a GET request or not.
The most common use of the HEAD request is when you are trying to decide whether to download a large file or not. You can do a HEAD request to show a user what file will be downloaded and then based on their response you can run the GET method or do nothing. This technique can save you some bandwidth, especially if the user decides not to download the file.
If you have some existing code and you aren't sure which request methods it supports, you can check by using the OPTIONS method. When you make this request, you will get a list of the other HTTP request methods that the web server will support. That could be helpful if you are trying to figure out if a method is making a PUT or POST request.
This is also helpful if you are working with APIs and you need to see which methods you can use. Once you have the list of approved HTTP request methods from your server, you can start making any changes you need to.
HTTP requests are something that we use all the time, but it's almost subconscious. We know how to use this stuff, but knowing a little about the concepts behind them makes it easier to appreciate them or use them better. I think the hardest difference for me to really get was the difference between POST and PUT.
Is there another request method that you use a lot that you don't see here? I know some people use the DELETE method, but I haven't seen a lot of cases for the TRACE or CONNECT methods. Anybody want to share their experience?
Hey! I'm doing a free webinar on June 23th over React. You'll learn everything you need to know to get setup and start working with React. If you haven't used React before, this will be a great introduction to it. If you have used React before, you might learn something new. You can go register for the webinar here