1

I got this test code:

import axios from 'axios';

const readWithAxios = async (basicAuth, user, passwd) => {
    let options = {
        auth: {
            username: user,
            password: passwd
        },
        headers: { "Content-Type": "application/json"},
        withCredentials: true
    };

    return axios.get('https:///geolite.info/geoip/v2.1/country/me?pretty', options);
}

const readWithFetch = (basicAuth) => {
    return new Promise(res=>{
        let headers = new Headers();
        headers.set('Authorization', basicAuth);

        fetch('https://geolite.info/geoip/v2.1/country/me?pretty', {
            method: 'GET',
            headers: headers,
        }).then(response => res(response.json()));
    })
}

const readData = async () => {
    let user = '<my_api_user>';
    let passwd = '<my_api_key>';
    let basicAuth = 'Basic ' + Buffer.from(user + ":" + passwd).toString('base64');

    let geoData;

    //geoData = await readWithFetch(basicAuth);
    geoData = await readWithAxios(basicAuth, user, passwd);

    console.log(geoData);
}
readData();

I'm trying to understand why readWithFetch works fine and axios gets connection refused. It's a simple basic auth... nothing fancy.

I've tried all these readWithAxios versions:

Version 1

const readWithAxios = async (basicAuth, user, passwd) => {
    let options = {
        auth: {
            username: user,
            password: passwd
        },
        headers: { "Content-Type": "application/json"},
        withCredentials: true
    };

    console.log('options', options);
    return axios.get('https:///geolite.info/geoip/v2.1/country/me?pretty', options);
}

Version 2

const readWithAxios = async (basicAuth, user, passwd) => {
    let options = {
        auth: {
            username: user,
            password: passwd
        },
        headers: { "Content-Type": "application/json", 'Authorization': basicAuth},
        withCredentials: true
    };

    console.log('options', options);
    return axios.get('https:///geolite.info/geoip/v2.1/country/me?pretty', options);
}

Version 3

const readWithAxios = async (basicAuth, user, passwd) => {
    let options = {
        method: 'GET',
        url: 'https:///geolite.info/geoip/v2.1/country/me?pretty',
        auth: {
            username: user,
            password: passwd
        },
        headers: { "Content-Type": "application/json", 'Authorization': basicAuth},
        withCredentials: true
    };

Version 4

    return axios(options);
}

const readWithAxios = async (basicAuth, user, passwd) => {
    let options = {
        method: 'GET',
        url: 'https:///geolite.info/geoip/v2.1/country/me?pretty',
        headers: { "Content-Type": "application/json", 'Authorization': basicAuth},
        withCredentials: true
    };

    return axios(options);
}

What is the correct way to write readWithAxios?

4
  • 1
    What are the HTTP responses? Axios withCredentials is used to send cookies. Commented Nov 16, 2022 at 2:43
  • All the examples I have seen have used the auth option by itself without the withCredentials option. Commented Nov 16, 2022 at 2:56
  • @OFRBG ECONNREFUSED, Commented Nov 16, 2022 at 3:14
  • @Geshode I tried without WithCredentials and the result was the same. Commented Nov 16, 2022 at 3:14

1 Answer 1

3

You have a triple slash in https:///geolite in the Axios versions. It should be https://

Sign up to request clarification or add additional context in comments.

3 Comments

OH MY GOD... I lost like 2h in this one! LOL Jesus!
Wow, good spot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.