JokerMartini
JokerMartini

Reputation: 6157

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...

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

Upvotes: 0

Views: 36

Answers (1)

Marco Balo
Marco Balo

Reputation: 336

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);

Upvotes: 1

Related Questions