0
0
Rest APIprogramming~5 mins

Why HTTP methods define intent in Rest API

Choose your learning style9 modes available
Introduction

HTTP methods tell the server what action you want to do with data. They make your request clear and help the server respond correctly.

When you want to get information from a website or app.
When you want to add new data to a server, like creating a new user.
When you want to change existing data, like updating a profile.
When you want to remove data, like deleting a post.
When you want to check if a resource exists without changing it.
Syntax
Rest API
GET /resource
POST /resource
PUT /resource
DELETE /resource
PATCH /resource

GET asks for data without changing it.

POST sends new data to the server.

PUT replaces existing data with new data.

DELETE removes data from the server.

PATCH updates parts of existing data.

Examples
This asks the server to send back all users.
Rest API
GET /users
// Retrieves a list of users
This sends new user data to the server to create a user.
Rest API
POST /users
{
  "name": "Alice"
}
// Adds a new user named Alice
This replaces the user data with new information.
Rest API
PUT /users/1
{
  "name": "Alice Smith"
}
// Updates user with ID 1
This tells the server to delete the user.
Rest API
DELETE /users/1
// Removes user with ID 1
Sample Program

This program shows how GET fetches data and DELETE removes it using HTTP methods.

Rest API
import requests

# URL of the API
url = 'https://jsonplaceholder.typicode.com/posts/1'

# GET request to fetch data
response_get = requests.get(url)
print('GET status:', response_get.status_code)
print('GET data:', response_get.json())

# DELETE request to remove data
response_delete = requests.delete(url)
print('DELETE status:', response_delete.status_code)
OutputSuccess
Important Notes

Using the right HTTP method helps servers understand your request clearly.

GET requests should never change data on the server.

POST, PUT, PATCH, and DELETE change data in different ways.

Summary

HTTP methods show what you want to do with data.

GET gets data, POST adds, PUT/PATCH update, DELETE removes.

Choosing the right method makes your API calls clear and safe.