โค Like
๐Ÿ”– Save
๐Ÿ”— Share
Eric Hu
Eric Hu

HTTP Methods

@thedevspaceio

๐ŸŒ 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.

MethodPurposeSafeIdempotentBody
GETRead a resource.โœ…โœ…โŒ
POSTCreate a resource.โŒโŒโœ…
PUTReplace a resource.โŒโœ…โœ…
PATCHPartially update a resource.โŒโŒโœ…
DELETEDelete a resource.โŒโœ…โŒ
HEADLike GET, but headers only.โœ…โœ…โŒ
OPTIONSDescribe 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.

http
GET /api/users/42 HTTP/1.1
http
HTTP/1.1 200 OK
Content-Type: application/json
 
{ "id": 42, "name": "Ada" }

Query parameters filter, sort, and paginate collections.

http
GET /api/users?role=admin&sort=name&page=2

POST

Create a new resource or trigger a non-CRUD action.

http
POST /api/users HTTP/1.1
Content-Type: application/json
 
{ "name": "Ada", "email": "ada@example.com" }
http
HTTP/1.1 201 Created
Location: /api/users/42

POST is not idempotent. Sending it twice creates two resources.


PUT

Replace a resource entirely. Fields you omit are reset.

http
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.

http
PATCH /api/users/42 HTTP/1.1
Content-Type: application/json
 
{ "email": "new@example.com" }

PUT vs PATCH

text
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.

http
DELETE /api/users/42 HTTP/1.1
http
HTTP/1.1 204 No Content

DELETE 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.

http
HEAD /api/files/report.pdf HTTP/1.1
http
HTTP/1.1 200 OK
Content-Length: 1048576
Content-Type: application/pdf

OPTIONS

Describes the communication options for a resource. Browsers send it automatically as a CORS preflight.

http
OPTIONS /api/users HTTP/1.1
http
HTTP/1.1 204 No Content
Allow: GET, POST, OPTIONS
Access-Control-Allow-Origin: https://example.com
Access-Control-Allow-Methods: GET, POST

Full-Stack AI Developer Roadmap

From HTML & CSS to working with AI models, all in one structured roadmap.

@thedevspaceio
www.thedevspace.io