
HTTP Methods
๐ HTTP Methods: The Verbs of Your API
HTTP methods define the action you want to perform on a resource. Think of them as verbs. They tell the server what to do with the data you're sending.
๐ GET: Fetch Data
What it does: Retrieves data without modifying anything. When to use: Fetching resourcesโuser profiles, product lists, search results. Key properties: Safe. Idempotent. Cacheable. What to remember: No body. Parameters go in the URL.
๐ฎ POST: Create Something New
What it does: Sends data to create a new resource. When to use: Submitting forms, creating accounts, adding products. Key properties: Not safe. Not idempotent. What to remember: Has a body. Each request is unique.
๐ฆ PUT: Replace Entirely
What it does: Replaces an entire existing resource. When to use: Full updates, synchronizing data. Key properties: Not safe. Idempotent. What to remember: Sends the complete resource. Missing fields get overwritten.
โ๏ธ PATCH: Update Partially
What it does: Applies partial updates to a resource. When to use: Updating specific fieldsโemail, password, settings. Key properties: Not safe. Not idempotent. What to remember: Sends only the fields to change.
๐๏ธ DELETE: Remove
What it does: Removes a resource from the server. When to use: Deleting accounts, removing posts, clearing cart items. Key properties: Not safe. Idempotent. What to remember: Often returns 204 No Content.
๐ HEAD: Headers Only
What it does: Fetches headers without the body. When to use: Checking existence, file size, cache validation. Key properties: Safe. Idempotent. Same as GET but no body. What to remember: Useful for discovery without transferring data.
๐ ๏ธ OPTIONS: Discover Available Methods
What it does: Describes communication options for a resource. When to use: Checking supported methods, CORS preflight. Key properties: Safe. Idempotent. What to remember: Tells you what methods you can use.
#http #api #rest #methods #webdev #backend #coding #tips
HTTP methods are used by the client tell the server what action to perform on a resource. Each method has specific semantics and properties.
| Method | Purpose | Safe | Idempotent | Body |
|---|---|---|---|---|
GET | Read a resource. | โ | โ | โ |
POST | Create a resource. | โ | โ | โ |
PUT | Replace a resource. | โ | โ | โ |
PATCH | Partially update a resource. | โ | โ | โ |
DELETE | Delete a resource. | โ | โ | โ |
HEAD | Like GET, but headers only. | โ | โ | โ |
OPTIONS | Describe allowed methods. | โ | โ | โ |
Safe: does not modify the resource. Idempotent: repeating the request has the same effect as making it once.
GET
Retrieve a resource. Never modifies data.
GET /api/users/42 HTTP/1.1HTTP/1.1 200 OK
Content-Type: application/json
{ "id": 42, "name": "Ada" }Query parameters filter, sort, and paginate collections.
GET /api/users?role=admin&sort=name&page=2POST
Create a new resource or trigger a non-CRUD action.
POST /api/users HTTP/1.1
Content-Type: application/json
{ "name": "Ada", "email": "ada@example.com" }HTTP/1.1 201 Created
Location: /api/users/42POST is not idempotent. Sending it twice creates two resources.
PUT
Replace a resource entirely. Fields you omit are reset.
PUT /api/users/42 HTTP/1.1
Content-Type: application/json
{ "name": "Ada Lovelace", "email": "ada@example.com" }PUT is idempotent. Sending the same request twice has the same result.
PATCH
Update specific fields of a resource. Fields you omit stay unchanged.
PATCH /api/users/42 HTTP/1.1
Content-Type: application/json
{ "email": "new@example.com" }PUT vs PATCH
PUT /users/42 { "name": "Ada" }
โ name is "Ada", email is now null (replaced entirely)
PATCH /users/42 { "name": "Ada" }
โ name is "Ada", email is unchanged (partial update)Use PUT for full replacement, PATCH for partial updates.
DELETE
Remove a resource.
DELETE /api/users/42 HTTP/1.1HTTP/1.1 204 No ContentDELETE is idempotent. Deleting the same resource twice has the same result.
HEAD
Like GET, but returns only headers. Useful for checking if a resource exists or its size.
HEAD /api/files/report.pdf HTTP/1.1HTTP/1.1 200 OK
Content-Length: 1048576
Content-Type: application/pdfOPTIONS
Describes the communication options for a resource. Browsers send it automatically as a CORS preflight.
OPTIONS /api/users HTTP/1.1HTTP/1.1 204 No Content
Allow: GET, POST, OPTIONS
Access-Control-Allow-Origin: https://example.com
Access-Control-Allow-Methods: GET, POSTFull-Stack AI Developer Roadmap
From HTML & CSS to working with AI models, all in one structured roadmap.