DEV Community

Victoria Lo
Victoria Lo

Posted on • Originally published at lo-victoria.com on

2

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
Enter fullscreen mode Exit fullscreen mode

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);
  });
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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!

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 like and share it around! Good luck with coding and cheers!

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay