Fetch and Axios are very similar in functionality, but for more backwards compatibility Axios seems to work better (fetch doesn't work in IE 11 for example, check this post)
Also, if you work with JSON requests, the following are some differences I stumbled upon with.
Fetch JSON post request
let url = 'https://someurl.com';
let options = {
method: 'POST',
mode: 'cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
},
body: JSON.stringify({
property_one: value_one,
property_two: value_two
})
};
let response = await fetch(url, options);
let responseOK = response && response.ok;
if (responseOK) {
let data = await response.json();
// do something with data
}
Axios JSON post request
let url = 'https://someurl.com';
let options = {
method: 'POST',
url: url,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
},
data: {
property_one: value_one,
property_two: value_two
}
};
let response = await axios(options);
let responseOK = response && response.status === 200 && response.statusText === 'OK';
if (responseOK) {
let data = await response.data;
// do something with data
}
So:
- Fetch's body = Axios' data
- Fetch's body has to be stringified, Axios' data contains the object
- Fetch has no url in request object, Axios has url in request object
- Fetch request function includes the url as parameter, Axios request function does not include the url as parameter.
- Fetch request is ok when response object contains the ok property, Axios request is ok when status is 200 and statusText is 'OK'
- To get the json object response: in fetch call the json() function on the response object, in Axios get data property of the response object.
Fetch and Axios are very similar in functionality, but for more backwards compatibility Axios seems to work better (fetch doesn't work in IE 11 for example, check this post)
Also, if you work with JSON requests, the following are some differences I stumbled upon with.
Fetch JSON post request
let url = 'https://someurl.com';
let options = {
method: 'POST',
mode: 'cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
},
body: JSON.stringify({
property_one: value_one,
property_two: value_two
})
};
let response = await fetch(url, options);
let responseOK = response && response.ok;
if (responseOK) {
let data = await response.json();
// do something with data
}
Axios JSON post request
let url = 'https://someurl.com';
let options = {
method: 'POST',
url: url,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
},
data: {
property_one: value_one,
property_two: value_two
}
};
let response = await axios(options);
let responseOK = response && response.status === 200 && response.statusText === 'OK';
if (responseOK) {
let data = await response.data;
// do something with data
}
So:
- Fetch's body = Axios' data
- Fetch's body has to be stringified, Axios' data contains the object
- Fetch has no url in request object, Axios has url in request object
- Fetch request function includes the url as parameter, Axios request function does not include the url as parameter.
- Fetch request is ok when response object contains the ok property, Axios request is ok when status is 200 and statusText is 'OK'
- To get the json object response: in fetch call the json() function on the response object, in Axios get data property of the response object.
They are HTTP request libraries...
I end up with the same doubt but the table in this post makes me go with isomorphic-fetch. Which is fetch but works with NodeJS.
Screenshot for convenience

I have gone crazy using fetch method, it is very hard for me, one of my coworker in my internship uses axios, i tried axios and learned api data fetching in like an hour, while in fetch it is hard for me to convert the data to response, so is it ok to use what I feel is easier for me? right now i am learning POST method but in fetch() i just dont understand the syntax
const handlePostTodo = (e) => {
e.preventDefault();
fetch("http://localhost:5000/todo", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ 0: newTodo }),
}).then(() => {
setNewTodo("");
fetchTodos();
});
console.log("post successful");
};
const fetchTodos = () => {
axios
.get("http://localhost:5000/todo")
.then((res) => setTodos(res.data))
.catch((err) => console.error(err));
};Is this a good practice? first i am using fetch for a POST request, but then to get that data from my db.json i am using axios, so i just want to know if i should use only axios or should i learn fetch? axios syntax feels easy for me to understand, and i dont know how to put a POST request in axios
TLDR: should i use axios or fetch, or whichever i feel is easy
sorry for yapping so much I am very frustrated right now😭
Videos
As i know it can basically do the same stuff for example api calls, post/get requests to server. Then why should i use a library for this when fetch is that easy to lern. If the only alternative were there goofy XmlHttpRequests i would say its ok to use a Library, but thats the reason why we have ajax with jquery