2023-11-29
3492
Faraz Kelhini
3494
Nov 29, 2023 â‹… 12 min read

How to make HTTP requests with Axios

Faraz Kelhini JavaScript developer.

Recent posts:

Exploring The Aha Stack: Astro, Htmx, Alpine — A Complete Tutorial With A Demo Project And Comparison To Other Stacks

Exploring the AHA stack: Tutorial, demo, and comparison

The AHA stack — Astro, htmx, and Alpine — is a solid web development stack for smaller apps that emphasize frontend speed and SEO.

Oyinkansola Awosan
May 3, 2024 â‹… 13 min read
Comparing Hattip Vs Express Js For Modern Application Development

Comparing Hattip vs. Express.js for modern app development

Explore what Hattip is, how it works, its benefits and key features, and the differences between Hattip and Express.js.

Antonello Zanini
May 2, 2024 â‹… 8 min read
Using React Shepherd To Build A Site Tour

Using React Shepherd to build a site tour

React Shepherd stands out as a site tour library due to its elegant UI and out-of-the-box, easy-to-use React Context implementation.

Onuorah Bonaventure
May 1, 2024 â‹… 14 min read
A Guide To Cookies In Next Js

A guide to cookies in Next.js

Cookies are crucial to web development. This article will explore how to handle cookies in your Next.js applications.

Georgey V B
Apr 30, 2024 â‹… 10 min read
View all posts

5 Replies to "How to make HTTP requests with Axios"

  1. You should also note that axios can also be used on the server with node.js – probably one of my favorite higher level HTTP libraries.

    One of the better qualities when using it on the server is the ability to create an instance with defaults – for example sometimes I’ll need to access another REST API to integrate another service with one of our products, if there is no existing package or the existing package doesn’t support the end points I need to access I’ll just create an abstraction which internally uses a http client created by axios.create():

    const instance = axios.create({
    baseURL: ‘https://api.example.org/’,
    headers: {‘Some-Auth-Header’: ‘token’}
    });

    Cheers,
    Chris

  2. This post says nothing about the responseType parameter, which can stream a large binary file.

  3. Got a question about accessing the data outside of the axios.spread. What I am doing is using node to collate some data from disparate API calls and return one dataset. I do the two calls, create a new object and return it.
    The new object exists within the AXIS code block but when I try and view outside it is blank.

    I also tried to do this in a function with a return but it also returns a blank.

    let retData = {};
    axios
    .all([reqDevInfo, reqConInfo])
    .then(
    axios.spread((resDevInfo,resConInfo ) => {
    retData.status = 200;
    retData.deviceName = deviceName
    retData.tenant = resDevInfo.data.results[0].tenant.name;
    retData.ru = resDevInfo.data.results[0].position;
    retData.TServerName = resConInfo.data.results[0].connected_endpoint.device.name;
    retData.TServerPort = resConInfo.data.results[0].cable.label;
    console.log(retData); // this print the expected data

    })
    )
    .catch(errors => {
    // react on errors.
    console.error(errors);

    });

    console.log(retData) // this is blank

  4. How can I build or append data elements to a post request before I send the request?
    I have optional 4 optional parameters and there are too many combinations to code for all the variations.

Leave a Reply