GET, POST, PUT, PATCH and DELETE
35 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A shop app needs one URL to do five different jobs: show a product, add one, replace it, change its stock and remove it. The method on the request says which job is meant.
GET: read data, with parameters in the URL
GET is the HTTP method that asks the server to send back a resource, such as a product or a list of products. It only reads and must never change anything on the server.
A search for products that match "redmi". The search word travels in the URL, and the request has no body.
Example 1 · GET a search with a query parameter
curl -i "http://localhost:8080/api/v1/products?q=redmi"HTTP/1.1 200 OK
Date: Fri, 16 Oct 2026 02:33:56 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 230
{
"content": [
{"id":1,"sku":"MOB-RN13","name":"Redmi Note 13 5G","categoryId":2,"categoryName":"Mobiles","price":17999.00,"stock":40,"isActive":true}
],
"page": 0,
"size": 20,
"totalElements": 1,
"totalPages": 1
}What this code does
- The request line is
GET /api/v1/products?q=redmi HTTP/1.1. The search word travels in the query string, and there is no request body. - The URL is in double quotes because
?and&mean something special to the shell. Quoting the URL is a good habit for every request with a query string. - A list comes back as one page: the matching products in
content, plus the page number (counted from 0), the page size and the totals.
A GET request has no body. The path picks the resource (/products/1) and the query string adds options (?q=redmi, ?maxPrice=2500), so a GET can be bookmarked, shared, cached and repeated.
URLs end up in browser history and server logs, so never put a password, OTP or token in a query string. Many servers also refuse very long URLs (often above about 8 KB); large inputs belong in a body.
POST: create something or trigger an action
POST is the HTTP method that sends data in the body for the server to process, most often to create a new item in a collection. The server picks the new id and usually answers 201 Created.
Create a product: the JSON goes in the body, and the client sends no id.
Example 2 · POST creates a product
curl -i http://localhost:8080/api/v1/products -H "Content-Type: application/json" -d '{"sku":"ACC-RKZ255","name":"boAt Rockerz 255 Pro+","categoryId":4,"price":1499.00,"stock":45}'HTTP/1.1 201 Created
Date: Fri, 16 Oct 2026 02:33:57 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 146
Location: http://localhost:8080/api/v1/products/16
{"id":16,"sku":"ACC-RKZ255","name":"boAt Rockerz 255 Pro+","categoryId":4,"categoryName":"Accessories","price":1499.00,"stock":45,"isActive":true}What this code does
-dputs the JSON in the body and makes curl sendPOST. The client sends no id: the server chooses it.- The answer is
201 CreatedwithLocation: http://localhost:8080/api/v1/products/16: the new product now lives at that URL, and the body shows it with its id.isActivewas left out, so the server used its default,true.
The answer carries a Location header with the address of the new item. POST is also the method for actions that do not fit the other verbs, such as placing an order, sending an OTP, adding an item to a cart or cancelling an order; each such POST can change server data.
In curl, -d adds a body and makes the request a POST; -H adds a header such as Content-Type: application/json.
PUT replaces a whole resource; PATCH changes part of it
PUT is the HTTP method that sends a complete new version of a resource to its URL, and the server replaces what it has with it. PATCH sends only the fields to change.
Product 16 updated three times: a PUT with every field, a PUT with only the price, and a PATCH with only the price.
Example 3 · PUT with a full body, PUT with one field, then PATCH
curl -i -X PUT http://localhost:8080/api/v1/products/16 -H "Content-Type: application/json" -d '{"sku":"ACC-RKZ255","name":"boAt Rockerz 255 Pro+ (Black)","categoryId":4,"price":1399.00,"stock":45,"isActive":true}'curl -i -X PUT http://localhost:8080/api/v1/products/16 -H "Content-Type: application/json" -d '{"price":1299.00}'curl -i -X PATCH http://localhost:8080/api/v1/products/16 -H "Content-Type: application/json" -d '{"price":1299.00}'$ curl -i -X PUT http://localhost:8080/api/v1/products/16 -H "Content-Type: application/json" -d '{"sku":"ACC-RKZ255","name":"boAt Rockerz 255 Pro+ (Black)","categoryId":4,"price":1399.00,"stock":45,"isActive":true}'
HTTP/1.1 200 OK
Date: Fri, 16 Oct 2026 02:33:58 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 154
{"id":16,"sku":"ACC-RKZ255","name":"boAt Rockerz 255 Pro+ (Black)","categoryId":4,"categoryName":"Accessories","price":1399.00,"stock":45,"isActive":true}
$ curl -i -X PUT http://localhost:8080/api/v1/products/16 -H "Content-Type: application/json" -d '{"price":1299.00}'
HTTP/1.1 400 Bad Request
Date: Fri, 16 Oct 2026 02:33:58 GMT
Content-Type: application/problem+json
Content-Length: 407
{"type":"https://shoplite.example/problems/validation-failed","title":"Validation failed","status":400,"detail":"The request has 5 invalid fields.","instance":"/api/v1/products/16","errors":[{"field":"sku","message":"is required"},{"field":"name","message":"is required"},{"field":"categoryId","message":"is required"},{"field":"stock","message":"is required"},{"field":"isActive","message":"is required"}]}
$ curl -i -X PATCH http://localhost:8080/api/v1/products/16 -H "Content-Type: application/json" -d '{"price":1299.00}'
HTTP/1.1 200 OK
Date: Fri, 16 Oct 2026 02:33:59 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 154
{"id":16,"sku":"ACC-RKZ255","name":"boAt Rockerz 255 Pro+ (Black)","categoryId":4,"categoryName":"Accessories","price":1299.00,"stock":45,"isActive":true}What this code does
- The first PUT sends all six fields, so the server replaces product 16 with this new version: new name, new price, everything else the same.
- The second PUT sends only the price. A PUT means "here is the whole product", so the server refuses with
400and lists every missing field inerrors. - The PATCH sends the same one-field body and works: only the price changes to 1299.00, and every other field stays as it was.
A PUT body must contain every field, even the ones you are not changing. A PATCH sends only what changes: {"price":1299.00} leaves the name, SKU and stock alone, which suits small edits from a form or an app.
The classic mistake is a PUT with only the changed field. A careful API rejects it with 400, as above; a careless one accepts it and silently empties the fields you left out.
PUT /files/report.pdf). Most JSON APIs, ShopLite included, create with POST and use PUT only to replace.DELETE removes; HEAD and OPTIONS
DELETE is the HTTP method that removes the resource at the URL. A successful delete usually answers 204 No Content, and a later GET for the same URL gives 404 Not Found.
Delete product 16 and ask for it again, then try HEAD and OPTIONS on a product that exists.
Example 4 · DELETE, then GET the same URL
curl -i -X DELETE http://localhost:8080/api/v1/products/16curl -i http://localhost:8080/api/v1/products/16$ curl -i -X DELETE http://localhost:8080/api/v1/products/16
HTTP/1.1 204 No Content
Date: Fri, 16 Oct 2026 02:34:00 GMT
$ curl -i http://localhost:8080/api/v1/products/16
HTTP/1.1 404 Not Found
Date: Fri, 16 Oct 2026 02:34:00 GMT
Content-Type: application/problem+json
Content-Length: 166
{"type":"https://shoplite.example/problems/not-found","title":"Resource not found","status":404,"detail":"Product 16 was not found.","instance":"/api/v1/products/16"}What this code does
-X DELETEsets the method. The answer204 No Contentmeans the delete worked and there is no body to send.- The product is gone, so a GET for the same URL now gets
404 Not Found. (ShopLite refuses to delete a product that appears in past orders, with409 Conflict; product 16 was never ordered.)
Example 5 · HEAD and OPTIONS
curl -I http://localhost:8080/api/v1/products/6curl -i -X OPTIONS http://localhost:8080/api/v1/products/6$ curl -I http://localhost:8080/api/v1/products/6 HTTP/1.1 200 OK Date: Fri, 16 Oct 2026 02:34:01 GMT Content-Type: application/json; charset=utf-8 Content-Length: 141 ETag: "p6-v1" Cache-Control: no-cache $ curl -i -X OPTIONS http://localhost:8080/api/v1/products/6 HTTP/1.1 204 No Content Date: Fri, 16 Oct 2026 02:34:02 GMT Allow: GET, HEAD, PUT, PATCH, DELETE, OPTIONS
What this code does
curl -IsendsHEAD. The headers are exactly what a GET would return, includingContent-Length: 141, but the 141-byte body is not sent.- OPTIONS answers
204withAllow: GET, HEAD, PUT, PATCH, DELETE, OPTIONS: the methods this URL supports.
HEAD is a GET that returns only the status line and headers, no body: a cheap way to check that something exists or how big it is. OPTIONS asks which methods a URL supports; the answer lists them in an Allow header. Browsers also send OPTIONS on their own before some cross-site requests, which the State, Cookies & Sessions module explains.
Picking the method for real actions
One question picks the method: does the request only read, create, replace, change part or remove? Three real ShopLite actions follow.
Example 6 · Update stock, place and cancel an order
curl -s -X PATCH http://localhost:8080/api/v1/products/7 -H "Content-Type: application/json" -d '{"stock":75}'curl -s http://localhost:8080/api/v1/orders -H "Content-Type: application/json" -d '{"customerId":1,"addressId":1,"items":[{"productId":7,"quantity":1}]}'curl -s -X POST http://localhost:8080/api/v1/orders/17/cancel$ curl -s -X PATCH http://localhost:8080/api/v1/products/7 -H "Content-Type: application/json" -d '{"stock":75}'
{"id":7,"sku":"ACC-PB20K","name":"Mi Power Bank 20000mAh","categoryId":4,"categoryName":"Accessories","price":2199.00,"stock":75,"isActive":true}
$ curl -s http://localhost:8080/api/v1/orders -H "Content-Type: application/json" -d '{"customerId":1,"addressId":1,"items":[{"productId":7,"quantity":1}]}'
{"id":17,"customerId":1,"addressId":1,"status":"PLACED","orderedAt":"2026-10-16T08:04:00+05:30","items":[{"productId":7,"sku":"ACC-PB20K","productName":"Mi Power Bank 20000mAh","quantity":1,"unitPrice":2199.00,"lineTotal":2199.00}],"total":2199.00}
$ curl -s -X POST http://localhost:8080/api/v1/orders/17/cancel
{"id":17,"customerId":1,"addressId":1,"status":"CANCELLED","orderedAt":"2026-10-16T08:04:00+05:30","items":[{"productId":7,"sku":"ACC-PB20K","productName":"Mi Power Bank 20000mAh","quantity":1,"unitPrice":2199.00,"lineTotal":2199.00}],"total":2199.00}What this code does
- Update stock is a PATCH with one field.
-s(silent) prints only the body here. - Placing an order is a POST to the orders collection: customer 1 buys one power bank for address 1, and the server creates order 17. Each such POST creates another order, so it is an action, not a fixed new state.
- Cancel is a POST to
/orders/17/cancel, an action URL: the server checks the rules, puts the power bank back in stock and returns the order with statusCANCELLED.
The method for four common actions:
- Search products →
GET /api/v1/products?q=redmi(only reads) - Add to cart (in a shop app that has a cart) →
POST /cart/items(each call adds; not a fixed new state) - Update stock →
PATCH /api/v1/products/7with{"stock":75}(one field) - Cancel an order →
POST /api/v1/orders/16/cancel(an action with rules: refund, restock)
Predict the output
Pick the method
Choose the method that fits each action.
Round 1 of 3
Which method fits?
405 Method Not Allowed and lists the methods it does allow in the Allow header.Next step
Check your understanding
12 quick questions