Lecture 16: PUT Method

REST API Development: Build, Test, Troubleshoot

The PUT method is used in REST APIs to update an existing resource. Unlike the POST method, which creates new data, PUT modifies data that already exists on the server.

Clients send updated information to the API, and the server replaces or updates the existing resource with the new values provided in the request.

Purpose of the PUT Method

PUT is used when a client needs to update a specific resource. The request typically includes the identifier of the resource along with the new data.

Common examples include:

Example PUT Request

A PUT request is usually sent to a specific endpoint that includes the identifier of the resource being updated.

PUT /api/products/5

Request body:

{ "id": 5, "name": "Wireless Mouse", "price": 22, "stock": 60 }

This request tells the server to update the product with ID 5 using the new values provided.

Server Processing

When the server receives a PUT request, it typically performs the following steps:

Example Response

If the update is successful, the server may return the updated resource or a confirmation message.

{ "id": 5, "name": "Wireless Mouse", "price": 22, "stock": 60 }

HTTP Status Codes for PUT

Testing PUT Requests

Developers can test PUT requests using tools such as Postman. These tools allow sending JSON data in the request body and verifying the API response.

Example endpoint tested in Postman:

https://localhost:5001/api/products/5

Best Practices

Following these practices helps maintain a reliable and predictable REST API.

Next Steps

After implementing the PUT method, the next step is to implement the DELETE method, which allows removing resources and completing full CRUD functionality within the API.