Uploading attachment using JavaScript
Answeredconst response = await axios.post(`uploads.json?filename=dummy.png`, body, {
baseURL: '/api/v2',
headers: {
common: {
Accept: 'application/json',
'Content-Type': 'image/png',
mode: 'no-cors',
Authorization: `Basic ${process.env.token}`,
},
},
withCredentials: true,
});
-
Hi james,
Can you try this?
Replace Content-Type'Content-Type': 'image/png',
with
'Content-Type': 'application/binary',
Hope this helps!!-sushant
-
Hello Sushant,
Thank you for your response but this still does not work. The image after uploading is still corrupt and can not be viewed.
Do you have a working example?
-
I haven't tried with Axios yet. but I have used attachment API in python scripts and it works as expected. you can give try with the postman client and check if that works for you and then you can troubleshoot the issue whether Axios is causing this or any other factors.-sushant
-
Hi @...
I'm using React with axios myself and I had the same issue which is now being resolved with the following:
First, the Content-Type must be binary, as Sushant suggested, only for the Upload API call. The Request API it will be the usual application/json:
'Content-Type': 'application/binary',
Second, I don't think you've converted your uploaded file to base64. You must convert before attaching it to your axios body. This way it won't break.
I have found this code snippet which works like a charm: https://codesandbox.io/s/convert-file-to-base64-in-react-lqi1e
It's written for React, but you should be able to extract a JS version of it.
Please sign in to leave a comment.
4 Comments