Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Turn http request into axios call

How do i convert the following http request into an axios call? The following call works correctly but i’m not familiar enough to convert it to an axios call.

POST http://localhost:3000/v1/storage/upload
Content-Type: multipart/form-data; boundary=----my-form-data
Authorization: Bearer {{token}}

------my-form-data
Content-Disposition: form-data; name="file"; filename="protrait.jpg"
Content-Type: image/jpg

< C:/Users/johndoe/Documents/portrait.jpg
------my-form-data--

I was trying to do where ‘file’ is the file object from a file input field.

const formData = new FormData();
formData.append({
    name: "file",
    filename: file.name,
});
const res = await api({
    method: "POST",
    url: "http://localhost:3000/v1/storage/upload",
    data: formData,
});

I get the following error…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TypeError: Failed to execute 'append' on 'FormData': 2 arguments required, but only 1 present.

>Solution :

append takes as input name and value, bur you are giving it in input only a dictionary with two couples name/value.

You could try the following code:

formData.append("name", "file");
formData.append("filename", file.name);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading