question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get a .post with 'Content-Type': 'multipart/form-data' to work

See original GitHub issue

I’ve spent a few hours today trying to get a post request to work with a few parameters and a file that I need to upload.

I was able to make it work with pure javascript and XMLHttpRequest but it doesn’t work with Axios. What am I doing wrong?

Here’s the code that works using XMLHttpRequest:

let data = new FormData();

data.append('action', 'ADD');
data.append('param', 0);
data.append('secondParam', 0);
data.append('file', new Blob([payload], { type: 'text/csv' }));

// this works
let request = new XMLHttpRequest();
request.open('POST', url);
request.send(data);

What would be the ‘Axios’ version of that?

Here’s one of my tries (the simple one):

// this won't work
const config = { headers: { 'Content-Type': 'multipart/form-data' } };
    axios.post(url, data, config)
    .then(response => console.log(response))
    .catch(errors => console.log(errors));

Thank you! And thanks for your great work with Axios!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:144
  • Comments:99 (9 by maintainers)

github_iconTop GitHub Comments

154reactions
nickuraltsevcommented, May 13, 2016

@rafaelbiten I’ve just tried to reproduce the issue but to no avail. I used the following code:

const data = new FormData();

data.append('action', 'ADD');
data.append('param', 0);
data.append('secondParam', 0);
data.append('file', new Blob(['test payload'], { type: 'text/csv' }));

axios.post('http://httpbin.org/post', data);

The data was successfully sent to the server:

screen shot 2016-05-12 at 9 12 19 pm
115reactions
ghostcommented, Aug 4, 2018

Two years latter: Same problem…

Read more comments on GitHub >

github_iconTop Results From Across the Web

'multipart/form-data' to work Axios React Native - Stack Overflow
Using the latest version of Axios, you make an HTTP request with 'Content-Type': 'multipart/form-data' in the header as follows:
Read more >
Having trouble sending an HTTP POST request with form-data ...
If I do specify Content-Type = "multipart/form-data", I get a 400 Bad Request error and no response/error message.
Read more >
How to post form with header multipart/form-data with HTTP ...
I need to post data to a service in form format (content-type as multipart/form-data). As per official document guided- we may set payload...
Read more >
Question: How to upload a file using HTTP POST Form-Data?
You'll need to use the HTTP content type "multipart/form-data" to do this, which Boomi doesn't have pre-defined. You'll need to type it into...
Read more >
[Multipart/form-data] Getting a wrong media file typer error on ...
Set the request content-type to "application/octet-stream" and check whether same content-type is allowed in webserver (supported) mime-types. 0. 1. 16 Jun 2021.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found