Skip to main content

Command Palette

Search for a command to run...

HTTP Methods

Published
2 min readView as Markdown
HTTP Methods

Common HTTP Methods in RESTful APIs

GET

Purpose

Used to retrieve data from the server without altering it.
Use Case: Ideal for fetching resources such as a list of products, details about a specific user, or fetching search results.
Example:

  • Request: GET /api/products (retrieves all products).

  • Request: GET /api/products/123 (retrieves a product with ID 123).
    Notes:

  • Safe method (no side effects on server data).

  • Can be cached for performance.

POST

Purpose

Sends data to the server to create a new resource.
Use Case: Used when creating new items such as user registration, adding a new blog post, or submitting a form.
Example:

  • Request: POST /api/products
    Payload (Body):
{ "name": "New Product", "price": 19.99 }
  • (This creates a new product with the given data).

  • Unlike GET, POST requests usually include a payload.

  • Often used when you need server-side processing like adding data to a database.

PUT

Purpose

Updates an existing resource, typically replacing the entire entity with the new data provided.
Use Case - Updating a user's profile information, replacing an existing record like modifying product details.

Example:

  • Request: PUT /api/products/123
    Payload (Body):
{ "name": "Updated Product", "price": 25.99 }
  • (This replaces the product with ID 123 with the new data).

    Key Difference from POST:

    PUT is idempotent (making the same PUT request multiple times will have the same result). POST is not idempotent (repeating it could create multiple resources).

DELETE

Purpose

Removes a resource from the server.
Use Case: Deleting a record, such as removing a user account or deleting a blog post.
Example:

  • Request: DELETE /api/products/123
    (This deletes the product with ID 123).
    Notes:

  • Like PUT, DELETE is idempotent (deleting a resource multiple times yields the same result).

PATCH

Purpose

Partially updates a resource, modifying only the fields provided.
Use Case: Used when you want to update part of a resource, such as changing just the price of a product or updating a user’s email address.
Example:

  • Request: PATCH /api/products/123
    Payload (Body):
{ "price": 24.99 }

Each method is designed to perform a specific action in RESTful APIs, allowing for clear, maintainable communication between clients and servers.