HTTP Methods: Definitions & Descriptions
HTTP Method | Definition / Description |
---|---|
GET | Retrieve data from a server at the specified resource. It is read-only and should have no side effects (i.e., not modify data). Commonly used for fetching lists, details, or static content. |
POST | Submit data to the server to create a new resource. The server processes the request and returns the new resource or a success status. Used in forms, sign-ups, data submissions. |
PUT | Replace an existing resource entirely with the provided data. If the resource does not exist, some APIs may create it (though this is debated). Used for complete updates. |
PATCH | Partially update an existing resource. Only the fields sent in the request are modified; others remain unchanged. More efficient than PUT for small changes. |
DELETE | Remove a resource from the server. It targets a specific resource or dataset. Often used for deleting users, posts, files, etc. |
HEAD | Same as GET , but only returns headers, not the response body. Useful for checking if a resource exists or retrieving metadata. |
OPTIONS | Returns allowed HTTP methods and CORS policies for a resource. Often used in preflight requests in cross-origin setups. |
TRACE | Echoes back the received request. Mostly used for debugging and testing, but generally disabled for security reasons. |
CONNECT | Establishes a tunnel, typically for HTTPS connections through proxies. Rarely used in app-level programming. |
Mastering HTTP Methods in Node.js (Express.js)
HTTP Method | Node.js Method (Express.js) | Usage Description | Typical Responses |
---|---|---|---|
GET | app.get('/route') |
Fetch data without causing side effects. |
200 OK , 204 No Content , 404 Not Found
|
POST | app.post('/route') |
Submit data to create a resource. |
201 Created , 400 Bad Request , 409 Conflict , 422 Unprocessable Entity , 500 Internal Server Error
|
PUT | app.put('/route/:id') |
Update/replace a full resource. |
200 OK , 204 No Content , 400 Bad Request , 404 Not Found
|
PATCH | app.patch('/route/:id') |
Partially update a resource. |
200 OK , 204 No Content , 400 Bad Request , 404 Not Found
|
DELETE | app.delete('/route/:id') |
Remove a resource by ID or criteria. |
200 OK , 204 No Content , 404 Not Found , 400 Bad Request
|
Typical HTTP Response Codes
Code | Meaning | When to Use |
---|---|---|
200 |
OK | Request succeeded (GET, PUT, PATCH, DELETE). |
201 |
Created | Resource created successfully (POST). |
204 |
No Content | Success with no body returned. |
400 |
Bad Request | Invalid input or request syntax. |
401 |
Unauthorized | Authentication is required or failed. |
403 |
Forbidden | Authenticated but not authorized. |
404 |
Not Found | Resource doesn’t exist. |
409 |
Conflict | Resource already exists or version conflict. |
422 |
Unprocessable Entity | Validation passed structurally but failed semantically. |
500 |
Internal Server Error | Unexpected error on server side. |
Top comments (0)