How to dynamically put data into a url in node js using axios

929 Views Asked by At

Hi since I am a novice this problem might be small but I could not find any solution.

    axios.get('http://apilayer.net/api/validateaccess_key=******&number=14158586273&country_code=&format=1')
    .then(response=>{
        console.log(response.data);
    }).catch(error =>{
        console.log(error)
    })

Now I wanted to make a bulk phone number validation program and I want to send different number through the url every time, the numbers are stored in an array. How to set a dynamic parameter to send different phone number from an array of numbers through the url?

1

There are 1 best solutions below

0
On

You can do like this:

const API_PATH = 'http://apilayer.net/api/';
const numbers = ['14158586273', '14158586274', '14158586275', ...]
const paramsData = {
    validateaccess_key: '******',
    number: numbers[0], // your desired number here
    country_code: 123,
    format: 1
}


axios.get(API_PATH, { params: paramsData })
  .then(response => {
    console.log(response.data);
  }).catch(error =>{
    console.log(error)
  })