PUT, POST and PATCH

PUT, POST and PATCH

ยท

2 min read

Table of contents

This article assumes that you have a basic understanding of REST API and HTTP verbs - GET, PUT, POST, PATCH, DELETE etc This article aims to explain the difference between the most used verbs for making changes to a resource and is not intended as an introduction to REST API and HTTP verbs.

PUT and POST are both HTTP verbs that can be used to create a new resource or update or replace an existing resource. While PATCH can be used to update parts of the resources.

PUT and POST are often used interchangeably when building a REST API, but maybe you shouldn't be doing that. Let us have a quick look at the original intent of building these HTTP verbs.

POST

The Post method creates a resource at a URL determined by the server.

An example of a sever-determined URL is /notes, the exact of the resource is not known, and a newly created resource will be stored in a sample address /notes/1

If another POST request is made to this URL, another resource will be created.

PUT

The PUT method creates a resource at a known URL determined by the client. E.g /notes/1 Since the request address is known, it is implied that there is most likely a resource at this address and the PUT method will modify/replace the resource at the address.

However, if there is no resource at the requested URL, then the PUT method is capable of creating a new resource at the address.

But, its original intent is that a PUT method is supplied with an address determined by the client, which indicates that it is better suited for making an update request.

Since PUT replaces the resource at the known URL if it already exists, sending the same request twice has no effect which means PUT is idempotent.

PATCH

Patch request works similarly to a PUT request but is used to update only part of a resource, unlike PUT which modifies the entire resource.

Happy coding ๐Ÿต

Additional Resource:

  1. Difference between PUT and POST

  2. w3g resource

  3. POST, PUT, PATCH