๐ HTTP Methods
1. What is the RESTful API ?โ
A RESTful API follows a standardized design style that facilitates communication between different systems on the network. To adhere to REST principles, an API should be predictable and easy to understand. As a frontend developer, the focus is primarily on three aspects:
- URL Path: Identifies the scope of the client's request, for example:
/products: Likely returns a product list/products/abc: Provides details for the product with ID "abc"
- HTTP Methods: Define the specific action to perform:
GET: Used to retrieve dataPOST: Used to create new dataPUT: Used to update existing dataDELETE: Used to delete data
- Status Codes: Provide a quick indication of whether the request was successful and, if not, where the problem might be. Common status codes include:
200: Success404: Requested resource not found500: Server error
2. If GET can also carry data in a request, why should we use POST?โ
Since GET can also send requests with data, why do we still need to use POST?
This is mainly based on four considerations:
- Security: Since GET data is appended to the URL, sensitive data is easily exposed. POST places data in the request body, which is relatively more secure.
- Data Size Limit: With GET, browsers and servers impose URL length limits (although it varies slightly between browsers, it generally falls around 2048 bytes), which restricts the amount of data. POST nominally has no limit, but in practice, middleware is typically configured to limit data size to prevent malicious attacks flooding with large payloads. For example, Express's
body-parser. - Semantic Clarity: Ensures developers can clearly understand the purpose of the request. GET is typically used for retrieving data, while POST is more suitable for creating or updating data.
- Immutability: In the HTTP protocol, the GET method is designed to be "safe" โ no matter how many requests are sent, there's no concern about modifying data on the server.
3. What does the PUT method do in HTTP?โ
What is the purpose of the PUT method?
It primarily serves two purposes:
- Update an existing resource (e.g., modifying user information)
- Create a new resource if it doesn't exist
Exampleโ
const axios = require('axios');
async function updateUser(userId, newName) {
const url = `https://api.example.com/users/${userId}`; // API URL
const data = {
name: newName,
};
try {
const response = await axios.put(url, data); // Execute PUT request
console.log('User updated:', response.data); // Output updated user info
} catch (error) {
console.log('Error updating user:', error); // Output error info
}
}
updateUser(1, 'Pitt Wu');