Difference between Fetch and Axios.js for making http requests

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. Fetch and Axios are very similar in functionality. Some developers prefer Axios over built-in APIs for its ease of use. The Fetch API is perfectly capable of reproducing the key features of Axios.

Fetch: The Fetch API provides a fetch() method defined on the window object. It also provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline (requests and responses). The fetch method has one mandatory argument- the URL of the resource to be fetched. This method returns a Promise that can be used to retrieve the response of the request.

  • Program:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    fetch('path-to-the-resource-to-be-fetched')
      .then((response) => {
      
        // Code for handling the response
      })
      .catch((error) => {
      
        // Code for handling the error
      });

    chevron_right

    
    

Axios: Axios is a Javascript library used to make HTTP requests from node.js or XMLHttpRequests from the browser and it supports the Promise API that is native to JS ES6. It can be used intercept HTTP requests and responses and enables client-side protection against XSRF. It also has the ability to cancel requests.



  • Progarm:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    axios.get('url')
      .then((response) => {
      
        // Code for handling the response
      })
      .catch((error) => {
      
        // Code for handling the error
      })

    chevron_right

    
    

Differences between Axios and Fetch:

Axios Fetch
Axios has url in request object. Fetch has no url in request object.
Axios is a stand-alone third party package that can be easily installed. Fetch is built into most modern browsers; no installation is required as such.
Axios enjoys built-in XSRF protection. Fetch does not.
Axios uses the data property. Fetch uses the body property.
Axios’ data contains the object. Fetch’s body has to be stringified.
Axios request is ok when status is 200 and statusText is ‘OK’. Fetch request is ok when response object contains the ok property.
Axios performs automatic transforms of JSON data. Fetch is a two-step process when handling JSON data- first, to make the actual request; second, to call the .json() method on the response.
Axios allows cancelling request and request timeout. Fetch does not.
Axios has the ability to intercept HTTP requests. Fetch, by default, doesn’t provide a way to intercept requests.
Axios has built-in support for download progress. Fetch does not support upload progress.
Axios has wide browser support. Fetch only supports Chrome 42+, Firefox 39+, Edge 14+, and Safari 10.1+ (This is known as Backward Compatibilty).

react-js-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.