What is GET, POST, PUT, DELETE etc; how is REST API related to this? Eg: what is the difference between POST vs PUT?

HTTP methods are standard actions used to manipulate resources on a server. They form a core part of REST API design, which utilizes these methods to provide clear mappings between CRUD (Create, Read, Update, Delete) operations and HTTP requests.

HTTP Methods Explained

  1. GET:
  2. Purpose: Retrieves data from a server at the specified resource. The GET method should be used only to fetch data and should have no other effect.
  3. Usage in REST: Corresponds to reading or retrieving a resource. It is idempotent, meaning multiple identical requests should have the same effect as a single request.
  4. POST:
  5. Purpose: Sends data to the server to create a new resource. The data is included in the body of the request. It is typically used when the server needs to create a new entry in a database.
  6. Usage in REST: Corresponds to creating a new resource. POST requests do not need to be idempotent, meaning subsequent identical requests may create multiple resources or trigger different actions.
  7. PUT:
  8. Purpose: Sends data to the server to update/replace an existing resource or create a resource if it does not exist. The data is included in the body of the request.
  9. Usage in REST: Corresponds to updating or replacing a resource. PUT requests are idempotent; thus, making the same request multiple times should not result in different outcomes.
  10. DELETE:
  11. Purpose: Deletes the specified resource from the server.
  12. Usage in REST: Corresponds to deleting a resource. It is also idempotent as deleting the same resource multiple times should result in the same state.

POST vs PUT: Key Differences

Understanding the differences between POST and PUT is crucial in RESTful API design, as they are both used to send data to the server but have distinct roles and behaviors:

  1. Idempotence:
  2. POST: Not idempotent. Making multiple identical POST requests will typically result in different outcomes, such as creating multiple entries in a database.
  3. PUT: Idempotent. Multiple identical PUT requests should result in a single resource being created or modified only once.
  4. Resource Identification:
  5. POST: Does not assume that the client knows the identifier (URL) of the resource. The server assigns a new resource ID and returns it to the client.
  6. PUT: Assumes that the client knows the exact URL of the resource. If the resource exists, it is replaced; if it does not, a new resource is created at the specified URL.
  7. Use Case:
  8. POST: Used when the server is responsible for determining the unique address of the new resource. Commonly used for scenarios where multiple resources might be created from a single request.
  9. PUT: Used when the client determines the URL, and the operation may involve updating or creating a resource at that URL.

Relation to REST API

In RESTful design, these HTTP methods provide the foundation for performing standardized actions on resources. By mapping CRUD operations to HTTP methods, RESTful APIs leverage the existing infrastructure and protocols of the web, making them easy to implement and understand. This mapping ensures that systems using REST APIs can communicate in a standardized way, improving interoperability and consistency across different systems.

Each method's semantics help developers understand what each API call is supposed to do, which aids in maintaining and testing APIs. By following these conventions, APIs also become more intuitive, allowing new developers to predict API behavior without extensive documentation.

Summary

In summary, GET, POST, PUT, and DELETE are essential HTTP methods used in RESTful APIs to perform specific actions on resources. Understanding the differences between these methods, especially between POST and PUT, is crucial for correctly implementing RESTful APIs. This adherence to standard methods enhances the effectiveness, scalability, and maintainability of web services.