NODEJS - AXIOS : Error : The "url" argument must be of type string. Received type object at Url.parse
Asked Answered
S

2

14

I am trying to fetch data from a rest API using AXIOS as below:

require('dotenv').config();
const axios = require('axios');

var url = 'https://931eb067-05c0-492a-8129-48ebfc27d426-bluemix.cloudant.com/dummy/_design/NEW_VIEW_DESIGN/_view/new-view?include_docs=true';
axios.get({url,auth: {
    username: process.env.account,
    password: process.env.password 
}}).then((res)=>{console.log(res.data);})
.catch((e)=>{console.log(e)});

I am able to access the metioned URL seperately by providing the credentials but I am getting the below error while using AXIOS

The "url" argument must be of type string. Received type object at Url.parse

What has gone wrong?

Scaffold answered 25/8, 2018 at 13:49 Comment(0)
P
16
axios.get({url,auth: {
    username: process.env.account,
    password: process.env.password
}}).then((res)=>{console.log(res.data);})

Should be

axios.get(url, { auth: {
    username: process.env.account,
    password: process.env.password
}}).then((res)=>{console.log(res.data);})
Pontefract answered 25/8, 2018 at 13:52 Comment(2)
The bracket must be before the name auth but you have it after it.Discomfort
as pointed out by Amr Saber, this answer contains invalid javascript, this answer is the correct oneMastodon
A
9

You have put url inside the config confie parameter but it must be before the config.

axios.get(url, {auth: {
    username: process.env.account,
    password: process.env.password
}}).then((res)=>{console.log(res.data);})
.catch((e)=>{console.log(e)});
Aborticide answered 25/8, 2018 at 13:54 Comment(3)
Since almost all sites use https whether you need to authenticate or not, why would the following code fail with the same error as the author, const url = "https://www.cia.gov/the-world-factbook/countries/"; const response = await axios.get(url); const $ = cheerio.load(response.data); The site does not require a login and I'm using async/await.Transmission
@MikeS. if you want seriously help, you need to address a new issue/question with full example and definitions. Using your code I get ReferenceError: cheerio is not defined.Aside
This issue is long past dead and buried. It’s not relevant anymore.Transmission

© 2022 - 2024 — McMap. All rights reserved.