I'm trying to upload an image to Cloudfare with the write API. Currently I'm using axios, because I was unable to achieve this with plain javascript. My code consists in:
export default async (req: NextApiRequest, res: NextApiResponse) => {
const response = await axios.get(
"https://www.datocms-assets.com/45562/test-image.png",
{ responseType: "stream" }
);
const form = new FormData();
form.append("image", response.data, "test.png");
try {
const postRes = await axios.post(
`https://api.cloudflare.com/client/v4/accounts/my-id/images/v1`,
form,
{
headers: {
...form.getHeaders(),
Authorization: `Bearer my-api-key`,
},
}
);
console.info("response", postRes);
} catch (error) {
console.error("Error while uploading to cloudflare", error);
}
};
I'm getting this error (sorry for the length):
request: <ref *1> ClientRequest {
...(more logs)...
_header: 'POST /client/v4/accounts/my-key(I removed it, but I'm using it)/images/v1 HTTP/1.1\r\n' +
'Accept: application/json, text/plain, */*\r\n' +
'Content-Type: multipart/form-data; boundary=--------------------------930843423466862063114860\r\n' +
'Authorization: Bearer my-key\r\n' +
'User-Agent: axios/0.26.1\r\n' +
'Host: api.cloudflare.com\r\n' +
'Connection: close\r\n' +
'Transfer-Encoding: chunked\r\n' +
'\r\n',
...(more logs)...
response: {
status: 400,
statusText: 'Bad Request',
headers: {
date: 'Wed, 30 Mar 2022 00:30:01 GMT',
'content-type': 'text/plain;charset=utf-8',
'transfer-encoding': 'chunked',
connection: 'close',
'cf-ray': '6f3cbbe73c67d2d4-EZE',
'cache-control': 'no-cache',
'set-cookie': [Array],
vary: 'Accept-Encoding',
'cf-cache-status': 'DYNAMIC',
'cf-images': 'err=5400',
'expect-ct': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
'x-content-type-options': 'nosniff',
server: 'cloudflare'
},
config: {
transitional: [Object],
adapter: [Function: httpAdapter],
transformRequest: [Array],
transformResponse: [Array],
timeout: 0,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
maxBodyLength: -1,
validateStatus: [Function: validateStatus],
headers: [Object],
method: 'post',
url: 'https://api.cloudflare.com/client/v4/accounts/my-id/images/v1',
data: [FormData]
},
Can anyone help me please? I'm losing my here with this haha. Thanks!