The Basics of HTTP Request Methods

The Basics of HTTP Request Methods

Hello coders! Today's tutorial is a simple one relating to back-end programming. If you haven't read it, please refer to my Beginner's Intro to Back-end Programming for some essential background info.

Okay, so let's dive into the topic~

Types of HTTP Requests

So if you recall, in my Beginner's Intro to Back-end Programming, I wrote that a HTTP Request is when a browser makes a request to the server to ask for some information. This is known as a 'GET' request.

But sometimes, the request may not be about retrieving info from the server, but also updating information to the server. This is called a 'PUT' request. Hence, they are called HTTP Request Methods to specify what type of action the browser wants to do with the server.

A HTTP Request method specifies the desired action the browser wants to do with the server.

So, there's a GET and PUT. What else?

Here's a simple and concise list of the common HTTP request methods.

GET: Retrieve and read data from server.

PUT: Update and replace data on the server.

POST: Creating data and submitting it to the server.

PATCH: Update and modify data on the server.

DELETE: Remove data from server.

Of course, note that this is a brief summary so I encourage you to learn more and start reading about it here.

Now that you know the common HTTP Request methods, let's try using them with an example.

Example: POST

As usual, let's install axios to make HTTP requests.

npm install axios

Then, let's say we want to create a new mail for an email app and POST it to our database. The code will be:

const axios = require('axios'); //so that we can use axios

axios.post('/send', {
    sender: 'victoria@example.com',
    receiver: 'person@example.com',
    subject: 'hello there',
    message: 'this is an example'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

And in our server.js file, we run npm install express to handle the request.

const express = require('express') //don't forget this
const app = express()

app.post('/send', function (req, res) {
   let sender = req.body.sender
   let receiver = req.body.receiver
   let subject = req.body.subject
   let message = req.body.message

   //do something here
})

app.listen(3000) //port number

And that is a simple example of a POST request. You can make the server do whatever you want it to do in the //do something here like maybe saving the posted mail to a database or sending it to the receiver. I have a mail sender tutorial about that actually (here it is!).

The possibilities of using GET, POST, PUT, PATCH and DELETE are endless and that's what makes back-end programming so useful in making powerful apps!

And that's all for now!

Thanks for taking the time to read this tutorial. I hope this is a helpful yet simple illustration to show the different types of HTTP request methods and how you can use them for your projects. It takes a little bit of practice but once you are familiar with them, you can build RESTful APIs! Maybe I'll make a tutorial on that someday~

Please ask any questions you have in the comments below. I am more than willing to help. If this is helpful in any way, please let me know with a 'thumbs up'! Good luck with coding and cheers!

Did you find this article valuable?

Support Victoria Lo by becoming a sponsor. Any amount is appreciated!

ย