RESTful APIs / HTTP Methods and Status Codes

Understanding HTTP Methods in REST APIs

This tutorial will guide you through the different HTTP methods used in REST APIs, including GET, POST, PUT, DELETE, PATCH, and OPTIONS.

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Covers the use of HTTP methods and appropriate status codes in REST APIs.

Understanding HTTP Methods in REST APIs

1. Introduction

Goal

This tutorial aims to help you understand the different HTTP methods used in REST APIs.

Learning Outcomes

By the end of this tutorial, you will be able to:
- Understand what HTTP methods are and how they function in REST APIs.
- Differentiate between GET, POST, PUT, DELETE, PATCH, and OPTIONS methods.
- Implement these methods in your own REST APIs.

Prerequisites

Basic knowledge of web development, HTTP, and APIs is recommended but not necessarily required.

2. Step-by-Step Guide

HTTP methods define the type of action a request intends to perform. They are sometimes referred to as HTTP verbs. The following are the most commonly used HTTP methods in REST APIs:

GET

This method retrieves information from the specified source. It doesn't change the state of the resource and is therefore considered safe. It should only retrieve data and should have no other effect.

POST

This method sends data to the server to create a new resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources.

PUT

This method is used to update a current resource with new data. The request will contain the updated data. The PUT method is idempotent. That means calling the same PUT request multiple times will always produce the same result.

DELETE

This method deletes the specified resource.

PATCH

This method is used to apply partial modifications to a resource.

OPTIONS

This method returns the HTTP methods that the server supports for the specified URL.

3. Code Examples

Let's look at some code snippets to understand these methods better.

GET Method

app.get('/users', function(req, res) {
  // Code to fetch users
  res.send('GET request to the users page');
});

In the above code, we define a GET request to fetch users. When the '/users' endpoint is hit with a GET request, the server responds by sending 'GET request to the users page'.

POST Method

app.post('/users', function(req, res) {
  // Code to create a new user
  res.send('POST request to the users page');
});

Here, we define a POST request to create a new user. The server responds by sending 'POST request to the users page'.

PUT Method

app.put('/users/:id', function(req, res) {
  // Code to update a user
  res.send(`PUT request to the user ${req.params.id}`);
});

This example shows a PUT request to update a user. The server responds by sending 'PUT request to the user {id}'.

DELETE Method

app.delete('/users/:id', function(req, res) {
  // Code to delete a user
  res.send(`DELETE request to delete user ${req.params.id}`);
});

This DELETE request deletes a user. The server responds by sending 'DELETE request to delete user {id}'.

PATCH Method

app.patch('/users/:id', function(req, res) {
  // Code to partially update a user
  res.send(`PATCH request to update user ${req.params.id}`);
});

This PATCH request partially updates a user. The server responds by sending 'PATCH request to update user {id}'.

OPTIONS Method

app.options('/users', function(req, res) {
  // Code to send the HTTP methods that the server supports for the specified URL
  res.send('OPTIONS request to the users page');
});

The OPTIONS request returns the HTTP methods that the server supports for the specified URL. The server responds by sending 'OPTIONS request to the users page'.

4. Summary

In this tutorial, we discussed the different HTTP methods used in REST APIs, including GET, POST, PUT, DELETE, PATCH, and OPTIONS. Each of these methods has a specific purpose and is important to understand when developing APIs.

5. Practice Exercises

  • Exercise 1: Create a REST API using the GET and POST methods to create and retrieve resources.
  • Exercise 2: Extend the API from exercise 1 to include PUT and DELETE methods to update and delete resources.
  • Exercise 3: Add options to handle OPTIONS and PATCH requests to the API from exercise 2.

These exercises will give you practical experience and reinforce what you've learned. Remember, the best way to learn is by doing. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Color Palette Generator

Generate color palettes from images.

Use tool

Lorem Ipsum Generator

Generate placeholder text for web design and mockups.

Use tool

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

AES Encryption/Decryption

Encrypt and decrypt text using AES encryption.

Use tool

Word to PDF Converter

Easily convert Word documents to PDFs.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help