I’m trying to use the upload images API in Cloudfare without any success.
I read the docs, but they are very poor and I can’t understand them at all. They also give me a CUrl that returns an error when you run it…
curl -X POST -F file=@./https://www.datocms-assets.com/45562/my-file.png -H "Authorization: Bearer my-key" https://api.cloudflare.com/client/v4/accounts/my-user-id/images/v1
Currently, my code consists in:
export default async (req: NextApiRequest, res: NextApiResponse) => {
const formData = new FormData();
formData.append(
"file",
request(
"https://www.datocms-assets.com/45562/my-file.png"
)
);
try {
await fetch(
"https://api.cloudflare.com/client/v4/accounts/my-user-id/images/v1",
{
method: "POST",
headers: {
Authorization: `Bearer my-key`,
"Content-Type": "multipart/form-data; boundary=abcd",
},
body: JSON.stringify(formData),
}
).then((uploadRes) => {
return res.status(200).send(
uploadRes.text().then((resolved) => {
console.log(resolved);
})
);
});
} catch (err) {
console.log(err);
}
};
And this returns an ERROR 5400: Bad request: incomplete multipart stream, and I can’t figure out what that is.
I also tried this:
export default async (req: NextApiRequest, res: NextApiResponse) => {
try {
await fetch(
"https://api.cloudflare.com/client/v4/accounts/c6e775850575d70aad58b5fdb9c1a248/images/v1",
{
method: "POST",
headers: {
Authorization: `Bearer b-Mpt1qldqNuM5cmcp3WP7_zqHhpxMdlaauj8ady`,
"Content-Type": "multipart/form-data; boundary=abcd",
},
body: JSON.stringify({
file: "https://www.datocms-assets.com/45562/1648253640-blog_scale_treehacks_cover.png"
}),
}
).then((uploadRes) => {
return res.status(200).send(
uploadRes.text().then((resolved) => {
console.log(resolved);
})
);
});
} catch (err) {
console.log(err);
}
};
But it doesn’t work neither.
Can someone help me please? thanks