12

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?

James Z
  • 12,209
  • 10
  • 24
  • 44
METALHEAD
  • 2,734
  • 3
  • 22
  • 37

2 Answers2

14
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);})
Mateus Gondim
  • 5,362
  • 6
  • 31
  • 51
Chris Cousins
  • 1,862
  • 8
  • 15
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)});
Gihan De Silva
  • 458
  • 8
  • 17
mcssym
  • 956
  • 5
  • 8
  • 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`. – Mike S. Mar 16 '22 at 12:58
  • @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`. – Timo Sep 03 '22 at 07:34
  • This issue is long past dead and buried. It’s not relevant anymore. – Mike S. Sep 04 '22 at 00:44