One of the fundamental tasks of any web application is to communicate with servers through the HTTP protocol. This can be easily achieved using Fetch or Axios.
The Fetch API is built-in most modern browsers and provides a fetch() method defined on the window object, while Axios is a specific 3rd party Javascript library you must install manually.
Which method do you usually use and do you think adding a library (around 4.4KB gzip) is worth it for the extra features it brings?
Oldest comments (28)
If the application needs to be light and have only a few http calls, then I would generally go for either
fetch
orxmlHTTPRequest
.For applications that are dependent on a lot of HTTP calls, then I think
axios
is the best approach and its easy to get away with setup.jQuery.ajax()
π€¦
That legendary function π
been there, done that ππ
axios
integrates better with TypeScript interfaces, however,fetch
(and well asnode-fetch
) has much more predictable output.Also,
fetch
can be attachedAbortController
. I would be interested in a library built on topfetch
.I know there are superagent and ky, but I never really tried.
What about?
Now
data
is an array of objectsFruit
Actually, it's just a fake typing.
It depends on the "real" data, whether it gets
JSON.parse()
or not.I use both, but mostly just stick with
fetch
andnode-fetch
πI would go with axios. It has nice API, and I never felt good with fetch. Dunno why, but axios feels so much more natural to my mind. It's really easy to use.
Same here π
Iβve moved from Axios to Fetch. Axios is easier to use, but Fetch doesnβt add anything extra. Therefore, Iβm mostly using Fetch now.
I prefer
axios
's API and alsofetch
doesn't throw an error when we get a non 200 response from the backend except for Network errors. Which is why I use redaxios, the best of both worlds.+1
Fetch can't handle 401 response, with no access to headers. Digest authentication is a problem.
My opinion is to use axios, because in some cases like if we have to show the progress of our request then it will be very difficult to handle this using fetch method but axios have a good managed function for that case.
Also axios provides good documentation for the function and there use.
fetch
is fine. I don't mind the 'doubleawait
' Usually, I move 'api' functionality to its own module/file anyway, so I am not typing it more than once.