A lightweight and flexible HTTP client for making API requests
HTTP Client
A simple HTTP client for making API requests.
Basic Usage
import http from 'http';
// GET request
const response = await http.get('/api/users');
// POST request with data
const newUser = await http.post('/api/users', {
name: 'John',
email: '[email protected]'
});
Available Methods
- GET:
http.get(url, config?)
- POST:
http.post(url, data, config?)
- PUT:
http.put(url, data, config?)
- PATCH:
http.patch(url, data, config?)
- DELETE:
http.delete(url, config?)
- HEAD:
http.head(url, config?)
Request Configuration
You can add headers and URL parameters to any request:
const response = await http.get('/api/users', {
headers: { 'Authorization': 'Bearer token' },
params: { page: '1', limit: '10' }
});
Response Format
All methods return a response with this structure:
{
data: any, // Response body
headers: {...}, // Response headers
status: number // HTTP status code
}
Error Handling
try {
const response = await http.get('/api/users');
} catch (error) {
console.error(error);
}