Sobes.tech
Junior

What HTTP request methods do you know?

sobes.tech AI

Answer from AI

Most common HTTP request methods:

  • GET: Requests a representation of the specified resource. Data is sent in the URL.

  • POST: Sends data to be processed to the specified resource. Data is sent in the request body.

  • PUT: Replaces all current representations of the target resource with the payload content.

  • DELETE: Deletes the specified resource.

  • PATCH: Applies partial modifications to a resource.

  • HEAD: Identical to GET but without the response body. Used to obtain metadata about the resource, such as headers.

  • OPTIONS: Used to describe the communication options for the target resource.

Examples of usage:

Method Description API Example
GET Retrieve data (e.g., list of users). GET /api/users
POST Create a new resource (e.g., user registration). POST /api/users (with user data in the request body)
PUT Fully update an existing resource (e.g., profile data update). PUT /api/users/{id} (with full user data in the request body)
DELETE Delete a resource (e.g., delete a user). DELETE /api/users/{id}
PATCH Partially update a resource (e.g., change only the user's email). PATCH /api/users/{id} (with partial update data in the request body)
HEAD Get resource headers without its body. HEAD /api/users
OPTIONS Get a list of allowed methods for the resource. OPTIONS /api/users
# Example of a GET request using the requests library
import requests

response = requests.get('https://api.example.com/users')
print(response.json())
// Example of a POST request using fetch API
fetch('https://api.example.com/users', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ name: 'John Doe', email: 'john@example.com' })
})
.then(response => response.json())
.then(data => console.log(data));