PUT method in REST API

The PUT method in a REST API is used to update an existing resource on the server, or create a new resource if it does not exist. It is typically used when a client wants to send data to the server to update an existing resource.

The PUT method is idempotent, meaning that making multiple identical requests will have the same effect as making a single request.

A typical PUT request will include the following:

  • The HTTP verb “PUT”
  • The endpoint or resource location, such as “/products/{product_id}”
  • The headers, which may include additional information such as the content type or the authentication information.
  • The request body, which contains the data that the client wants to send to the server in order to update the resource.

A typical PUT response will include the following:

  • The HTTP status code, such as 200 OK if the request was successful or 400 Bad Request if the request was malformed or invalid.
  • The headers, which may include additional information such as the location of the new resource or the content type.
  • The response body, which may contain the updated resource or additional information about the status of the request.

It’s worth noting that the PUT method is typically used to send data to the server to update an existing resource, but it could also be used to create a new one if it does not exist, depending on the API’s design.

Additionally, the PUT method is idempotent, which means that making multiple identical requests will have the same effect as making a single request.

It’s also important to note that the PUT method expects to receive the entire representation of the resource in the request body, including all fields, and it will replace the current resource with the new representation, this is why it’s not recommended to use it for partial updates, that’s why PATCH method was created for this purpose.