Feature | Axios | Fetch |
---|---|---|
Ease of Use | Simpler syntax, | more verbose |
Error Handling | Handles HTTP errors automatically (rejects on non-2xx) | Requires manual error handling (res.ok ) |
Timeout Support | Built-in support | Requires extra code using AbortController |
Automatic JSON Handling | Yes, responses are auto-parsed | No, need res.json() manually |
Default Headers | Sends headers like Content-Type automatically |
Requires manual headers setup |
File Uploads | Handles multipart/form-data easily |
More complex setup needed |
Performance | Slightly heavier (extra features) | Lighter and built-in |
async function main() {
const res = await fetch(
"<https://httpdump.app/dumps/29a55f87-0dac-444c-9b27-4e33369ff548>",
{
method: "post",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer token87689",
},
body: JSON.stringify({ username: "suphal" }),
}
);
const data = await res.json();
}
main();
import axios from 'axios';
async function fetchData() {
try {
const response = await axios.get('<https://api.example.com/data>');
console.log(response.data);
} catch (error) {
console.error('Error fetching data:', error);
}
}
fetchData();
http dum for checking request body , headers are going or not