Activity 29: HTTP Methods

HTTP Methods in RESTful APIs

RESTful APIs rely on HTTP methods to perform actions on resources. Each method corresponds to a specific type of operation. Below is an overview of the most commonly used methods:

  1. GET

  • Purpose: Retrieve data from a server.

  • Characteristics:

    • Does not modify server data (read-only).

    • Can include query parameters to filter or search resources.

    • Responses are typically cached unless specified otherwise.

Example:

  • URL: GET /api/products

  • Use Case: Retrieve a list of all products.

[
  { "id": 1, "name": "Laptop", "price": 1200 },
  { "id": 2, "name": "Mouse", "price": 25 }
]
  • URL: GET /api/products/1

  • Use Case: Fetch details of the product with ID 1.

  • When to Use:

    • Fetching a list of resources (e.g., users, products, posts).

    • Retrieving detailed information about a specific resource.

  1. POST

  • Purpose: Send data to the server to create a new resource.

  • Characteristics:

    • The request body contains the data for the resource to be created.

    • Returns 201 Created on success, often with a link to the newly created resource.

  • Example:

    • URL: POST /api/products

    • Request Body:

{ "name": "Keyboard", "price": 50 }
  • Response:
{ "id": 3, "name": "Keyboard", "price": 50 }
  • When to Use:

    • Creating a new user, product, or post in a database.

    • Sending form data, such as registration forms.

  1. PUT

    • Purpose: Update an existing resource or create one if it doesn't exist.

    • Characteristics:

      • Typically replaces the entire resource with the data provided.

      • If the resource does not exist, it may create a new one (idempotent behavior).

    • Example:

      • URL: PUT /api/products/1

      • Request Body:

    { "name": "Laptop", "price": 1100 }
  • Response:
{ "id": 1, "name": "Laptop", "price": 1100 }
  • When to Use:

    • Replacing or updating a resource entirely.

    • Example: Updating a product’s details such as price or description.

  1. DELETE

    • Purpose: Remove a resource from the server.

    • Characteristics:

      • Does not typically return a response body (status code 204 No Content).

      • Should only remove the specified resource.

    • Example:

      • URL: DELETE /api/products/1

      • Use Case: Delete the product with ID 1.

      • Response:

HTTP/1.1 204 No Content
  • When to Use:

    • Removing a resource permanently from the server.

    • Example: Deleting a user account, removing a blog post.

  1. PATCH

    • Purpose: Apply partial updates to a resource.

    • Characteristics:

      • Unlike PUT, it does not require sending the entire resource data.

      • Only the fields to be updated are sent in the request.

    • Example:

      • URL: PATCH /api/products/1

      • Request Body:

          jsonCopy code{ "price": 1150 }
        
      • Response:

          jsonCopy code{ "id": 1, "name": "Laptop", "price": 1150 }
        
  • When to Use:

    • Making minor changes to a resource without modifying all its fields.

    • Example: Updating a single field like the price of a product.

Best Practices for Using HTTP Methods

  1. Use the Right Method for the Action:

    • Follow RESTful conventions (e.g., use GET for fetching data, not POST).
  2. Status Codes:

    • Return appropriate HTTP status codes (200, 201, 404, etc.).
  3. Secure the API:

    • Use HTTPS and secure tokens for sensitive operations like POST, PUT, and DELETE.
  4. Pagination:

    • Use query parameters for GET requests to handle large datasets (?page=1&limit=10).
  5. Error Handling:

    • Provide meaningful error messages for bad requests or unauthorized access.
  6. Validation:

    • Validate data for POST and PUT requests to prevent malformed data entry.

REFERENCES:

https://pierek.com/blog/understanding-rest-api-methods-get-post-patch-put-and-delete-with-express-js-typescript-examples

https://www.freecodecamp.org/news/http-request-methods-explained/