unable to upload file through box api from express
app.post('/upload', async (req, res) => {
try {
const file = req.files.file;
const ACCESS_TOKEN = req.cookies.access_token;
const url = 'https://upload.box.com/api/2.0/files/content';
const fileData = fs.promises.open(file.tempFilePath);
fs.promises.read(fileData);
const response = await axios.post(
url,
{
attributes: {
name: file.name,
parent: { id: '0' },
content_created_at: `${new Date()}`,
content_modified_at: `${new Date()}`
}
},
{ file: form },
{
headers: {
Authorization: `Bearer ${ACCESS_TOKEN}`,
'Content-Type': 'multipart/form-data'
},
withCredentials: true
}
);
// const data = file.data;
res.status(200).json({ success: true, response });
} catch (error) {
//console.log(error);
res.status(400).json(error);
}
});
while uploading file getting this error
{"message": "Request failed with status code 415","name": "AxiosError","stack": "AxiosError: Request failed with status code 415\n at settle (/home/rohit/Opayl/learnNode/opyalBox/node_modules/axios/dist/node/axios.cjs:1896:12)\n at IncomingMessage.handleStreamEnd (/home/rohit/Opayl/learnNode/opyalBox/node_modules/axios/dist/node/axios.cjs:2940:11)\n at IncomingMessage.emit (node:events:525:35)\n at endReadableNT (node:internal/streams/readable:1359:12)\n at process.processTicksAndRejections (node:internal/process/task_queues:82:21)","config": {"transitional": {"silentJSONParsing": true,"forcedJSONParsing": true,"clarifyTimeoutError": false},"adapter": ["xhr","http"],"transformRequest": [null],"transformResponse": [null],"timeout": 0,"xsrfCookieName": "XSRF-TOKEN","xsrfHeaderName": "X-XSRF-TOKEN","maxContentLength": -1,"maxBodyLength": -1,"env": {},"headers": {"Accept": "application/json, text/plain, */*","Content-Type": "application/json","User-Agent": "axios/1.3.2","Content-Length": "212","Accept-Encoding": "gzip, compress, deflate, br"},"file": {},"method": "post","url": "https://upload.box.com/api/2.0/files/content","data": "{\"attributes\":{\"name\":\"new.txt\",\"parent\":{\"id\":\"0\"},\"content_created_at\":\"Wed Feb 15 2023 10:08:29 GMT+0530 (India Standard Time)\",\"content_modified_at\":\"Wed Feb 15 2023 10:08:29 GMT+0530 (India Standard Time)\"}}"},"code": "ERR_BAD_REQUEST","status": 415}
need help
-
Hi There,
It looks like something is wrong with the request, but I can't quite tell what. Can you review our documentation and use some of the sample code shown on there - then try again?
https://developer.box.com/guides/uploads/direct/file/
https://developer.box.com/reference/post-files-content/
Thanks
Please sign in to leave a comment.
Comments
1 comment