I want to fetch an image from an URL and then upload it to Cloudflare Images. But I keep getting a 400 error. Can someone please take a look at my code and tell me what’s wrong with it?
const response = await axios.get(url, { responseType: 'stream' });
const form = new FormData();
// Pass image stream from response directly to form
form.append('image', response.data, 'kitten.jpg');
try {
const postRes = await axios.post(`https://api.cloudflare.com/client/v4/accounts/${process.env.ACCOUNT_ID}/images/v1`, form, {
headers: {
...form.getHeaders(),
Authorization: `Bearer ${process.env.API_KEY}`,
},
});
console.info('response', postRes);
} catch (error) {
console.error('Error while uploading to cloudflare', error);
}
Here is the error I’m getting:
Error while uploading to cloudflare Error: Request failed with status code 400