Trending
- A Deep Dive into Tracing Agentic Workflows (Part 2)
- Engineering Closed-Loop Graph-RAG Systems, Part 3: Closing the Loop in Graph-RAG Systems
- The Big Data Architecture Blueprint: Core Storage, Integration, and Governance Patterns
- Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
- DZone
- Data Engineering
- Databases
- Do You Need PUT and PATCH?
Do You Need PUT and PATCH?
Find out if your really need PUT and PATCH.
Join the DZone community and get the full member experience.
Join For FreeConventional wisdom says that REST APIs should be implemented as follows:
-
GETโ read -
POSTโ add -
PUT/PATCHโ modify -
DELETEโ remove
This mostly works well. Query parameters can be used to supply arguments for GETand DELETEoperations. POSTs can use either URL-encoded or multipart form data, standard encodings supported by nearly all web browsers, and other HTTP clients.
However, it doesn't work quite as well for PUT or PATCH. PUThas no standard encoding and requires the entire resource to be sent in the payload. PATCHwas introduced as a workaround to this limitation, but it also lacks a standard encoding and is not supported by all clients (notably Java).
However, the POST method can also be used to modify resources. The semantics of POSTare less strict than PUT, so it can support partial updates like PATCH. Further, the same encoding used for creating resources (either URL-encoded or multipart) can also be used for updates.
For example:
POST /productsโ add a new resource to the "products" collection using the data specified in the request bodyPOST /products/101โ update the existing resource with ID 101 in the products collection using the (possibly partial) data specified in the request body
This approach works particularly well when resources are backed by relational database tables. An "add" POSTmaps directly to a SQL INSERToperation, and a "modify" POSTtranslates to a SQL UPDATE. The key/value pairs in the body (whether URL-encoded or multipart) map directly to table column names and values.
The approach also supports bulk inserts and updates. POSTing a URL-encoded payload works well for individual records, but JSON, CSV, or XML could easily be used to add or update multiple records at a time.
So, do you really need PUTand PATCH? Given that POST is more flexible, better supported, and can handle both create and update operations, I'd say no. Please share your thoughts in the comments!
Opinions expressed by DZone contributors are their own.
Related
-
Applying Oracle 19c Release Update (RU): A Practical Guide from My DBA Experience
-
The Aggregate Reference Problem
-
The Serverless Ceiling: Designing Write-Heavy Backends With Aurora Limitless
-
Jakarta Query: Unifying Queries Across SQL and NoSQL in Jakarta EE 12
