Uploading Images into Cloudflare using node.js

I am trying to upload an image into Cloudflare using node.js; however, I keep getting this error
errors: [
{
code: 5455,
message: 'Uploaded image must have image/jpeg, image/png, image/webp, image/gif or image/svg+xml content-type'
}
],
errors: [
{
code: 5455,
message: 'Uploaded image must have image/jpeg, image/png, image/webp, image/gif or image/svg+xml content-type'
}
],
My current function for the upload is
async function uploadImageToCloudFlare(path)
{
const fileData = fs.readFileSync("src/uploads/dog.jpeg")

const API_KEY = process.env.CLOUDFLARE_API_KEY
const ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID

const url = `https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/images/v1`;

const headers = {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'multipart/form-data',
};

const formData = new FormData();
formData.append('file', fileData);

const response = await axios.post(url, formData, {headers})
return response;
}
async function uploadImageToCloudFlare(path)
{
const fileData = fs.readFileSync("src/uploads/dog.jpeg")

const API_KEY = process.env.CLOUDFLARE_API_KEY
const ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID

const url = `https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/images/v1`;

const headers = {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'multipart/form-data',
};

const formData = new FormData();
formData.append('file', fileData);

const response = await axios.post(url, formData, {headers})
return response;
}
The directory the images are in is in the picture. I don't think it's any trouble with retrieving the image. I have tried making content-type the same content type as file request I have tried the sme thing on fetch since I heard there were some inconsistencies with axios I have also checked the properties of the file manually and it is a jpeg file
No description
1 Reply
greatalts
greataltsOP10mo ago
After 15 hours of research and chatgpt giving me the wrong information. i figured out the function
const axios = require("axios")
const fs = require('fs')
const FormData = require('form-data');

async function uploadImageToCloudFlare(path)
{
const form = new FormData();
form.append('file', fs.readFileSync(path), path);

const API_KEY = process.env.CLOUDFLARE_API_KEY
const ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID

const response = await axios.post(
`https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/images/v1`,
form,
{
headers: {
...form.getHeaders(),
'Authorization': `Bearer ${API_KEY}`
}
}
);
return response;
}
const axios = require("axios")
const fs = require('fs')
const FormData = require('form-data');

async function uploadImageToCloudFlare(path)
{
const form = new FormData();
form.append('file', fs.readFileSync(path), path);

const API_KEY = process.env.CLOUDFLARE_API_KEY
const ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID

const response = await axios.post(
`https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/images/v1`,
form,
{
headers: {
...form.getHeaders(),
'Authorization': `Bearer ${API_KEY}`
}
}
);
return response;
}
If anyone struggles with this in the future
Want results from more Discord servers?
Add your server