axios The "url" argument must be of type string. Received type undefined error
You are using destructuring:
await axios({
downloadUrl,
responseType: "stream"
})
This means, You are using downloadUrl
as key, instead of url
:
await axios({
downloadUrl: downloadUrl,
responseType: "stream"
})
You need to change it to url
:
await axios({
url: downloadUrl,
responseType: "stream"
})
A proper example of axios
from the doc:
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});
nadav atar
Updated on June 09, 2022Comments
-
nadav atar almost 2 years
Im working on an electron app that is trying to download a photo from the unsplash API and set it as a wallpaper. When I call the API I get 200 OK status and get the download URL, but when I try to download the photo with the axios stream method I get the following error:
UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received type undefined
this is the function code:
ipcMain.on("getRandomWallpaper", async event => { const randomApi = `${apiGateway}/?client_id=${unsplashKey}`; const request = await axios({ method: "get", url: randomApi }); if (request.status === 200) { const downloadUrl = request.data.links.download; const imagePath = "./images"; const download_image = async (downloadUrl, imagePath) => { await axios({ downloadUrl, responseType: "stream" }).then( response => new Promise((resolve, reject) => { response.data .pipe(fs.createWriteStream(imagePath)) .on("finish", () => resolve()) .on("error", e => reject(e)); }) ); }; download_image(downloadUrl, imagePath); } else { const status = request.status; console.error(`${status}: \n Something went wrong...`); } });
When I tried to console.log the downloadUrl parameter inside the function it printed a value. Also I did
console.log(typeoff(downloadUrl))
and it printed string. I hope you can help me, thanks in advance.
-
GGEv over 4 yearsThe thing is if your url string variable is named "url" it can be passed directly without a key-pair structure, but in the case the url link variable is named otherwise you need a pair-key structure