HTTP methods define the action a client wants to perform on a resource, like retrieving, modifying, or deleting it. These methods are language-independent because they are part of the HTTP protocol. However, how they are implemented or used can vary slightly across programming languages and frameworks. π
Common HTTP Methods π₯οΈ
1. GET
- Purpose: Retrieve data from a server without modifying it.
- When to Use:
- Fetching a list of resources (e.g., articles, users).
- Retrieving details of a specific resource (e.g., a user profile).
- Idempotent: Yes (calling it multiple times doesn’t change the resource).
- Example:
- URL:
GET /users/123
to fetch user data. - Browser: Loading a webpage or an image.
- Code Example (Python):
import requests response = requests.get('https://api.example.com/users/123') print(response.json())
- URL:
2. POST
- Purpose: Send data to the server to create a new resource.
- When to Use:
- Submitting a registration form.
- Uploading a file.
- Idempotent: No (calling it multiple times creates duplicates unless handled).
- Example:
- URL:
POST /users
with a JSON body:{"name": "John", "email": "john@example.com"}
. - Code Example (Node.js):
const axios = require('axios'); axios.post('https://api.example.com/users', { name: 'John', email: 'john@example.com' }) .then(response => console.log(response.data));
- URL:
3. PUT
- Purpose: Replace an existing resource or create it if it doesnβt exist.
- When to Use:
- Replacing all data of a resource.
- Updating configurations.
- Idempotent: Yes (subsequent calls result in the same resource).
- Example:
- URL:
PUT /users/123
with a JSON body:{"name": "John", "email": "john.doe@example.com"}
. - Code Example (Java):
HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://api.example.com/users/123")) .PUT(HttpRequest.BodyPublishers.ofString("{\"name\":\"John\",\"email\":\"john.doe@example.com\"}")) .build();
- URL:
4. PATCH
- Purpose: Partially update an existing resource.
- When to Use:
- Modifying a single field (e.g., updating just a user’s email).
- Idempotent: Yes.
- Example:
- URL:
PATCH /users/123
with a JSON body:{"email": "new.email@example.com"}
. - Code Example (Ruby):
require 'net/http' require 'uri' require 'json' uri = URI.parse("https://api.example.com/users/123") http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Patch.new(uri.request_uri) request.body = {email: "new.email@example.com"}.to_json http.request(request)
- URL:
5. DELETE
- Purpose: Remove a resource from the server.
- When to Use:
- Deleting a user account or a specific record.
- Idempotent: Yes.
- Example:
- URL:
DELETE /users/123
. - Code Example (Go):
client := &http.Client{} req, _ := http.NewRequest("DELETE", "https://api.example.com/users/123", nil) client.Do(req)
- URL:
6. OPTIONS
- Purpose: Retrieve supported HTTP methods for a specific resource.
- When to Use:
- Checking API capabilities (e.g., allowed methods on an endpoint).
- Idempotent: Yes.
- Example:
- URL:
OPTIONS /users
.
- URL:
7. HEAD
- Purpose: Similar to GET but retrieves only headers, not the body.
- When to Use:
- Checking if a resource exists (e.g., before downloading a file).
- Idempotent: Yes.
- Example:
- URL:
HEAD /users/123
.
- URL:
8. TRACE
- Purpose: Echoes the received request, used for debugging.
- When to Use:
- Diagnosing issues with proxies or intermediate servers.
- Idempotent: Yes (used for diagnostics, not modifying resources).
9. CONNECT
- Purpose: Establish a tunnel to the server, typically for HTTPS connections.
- When to Use:
- Rarely used directly by developers but important for proxying or secure connections.
Key Considerations Across Languages π οΈ
HTTP methods themselves are universal and implemented consistently across languages. However:
- Syntax: Each language/framework has its own libraries or modules for handling HTTP requests.
- Python:
requests
orhttp.client
. - JavaScript:
fetch
oraxios
. - Java:
HttpClient
orRestTemplate
.
- Python:
- Integration: Frameworks like Django (Python), Spring Boot (Java), or Express.js (Node.js) simplify using these methods.
When to Use Which HTTP Method
- GET: Fetch data without side effects.
- POST: Create new resources or perform actions with side effects.
- PUT: Replace an entire resource or create one if missing.
- PATCH: Partially update a resource.
- DELETE: Remove a resource.
- HEAD: Verify a resource exists without fetching data.
- OPTIONS: Discover server capabilities.
- TRACE: Debugging communication paths.