Hapi

API Design: PUT vs PATCH


I was writing an API and couldn't decide which HTTP verb to use to update an object. So this is just a quick post to clarify the difference as I understand it. In my example, the object will be a person.

A person consists of;

"person": { "age": 22, "name": "joe" }

PUT

Use this when you are going to send the complete object and all its properties in the request, so if we have a person object above, and you are wanting to change the age using a PUT, you would still send all the other properties with their existing values.

"person": { "age": 24, "name": "joe" }

PATCH

This is used when you want to update specific values or properties of an object. In the example above, to change the age of a person with a PATCH request you only send the age property with its new value.

"person" : { "age" : 12 }

Of course there is no real enforcement of this principle, and even using PUT/PATCH for any sort of update is still closer to standard RESTful practice, but if you want to keep things elegant and in-keeping with best practice this should help.


architecture
api

A tech native with 20 years of experience across the digital space. Darryl is an evangelist for the power and disruption of blockchain technologies and fosters a passion for bringing ever greater utility and adoption to the masses.

Related Articles

The Importance of a Robust API

Accelerating Business Growth: The Crucial Role of an Agile API

Hapi

Returning Custom Headers With Hapi JS

CTO-as-a-Service

Driving Startup Success: The Impact of CTO-as-a-Service

Copyright © 2016-2024 Bonbon Group Ltd. All Rights Reserved.