1

I need to send POST request with MIME - multipart/form-data

This is my default configuration for POST headers: axios.defaults.headers.post['Content-Type'] = 'multipart/form-data';

I expect that default Content-Type should be multipart/form-dat, but in chrome devtools I see Content-Type: application/json

4
  • To be clear...is that application/json in the request headers and not the response ones? Commented Apr 6, 2019 at 15:59
  • Yes, in the request headers Commented Apr 6, 2019 at 16:00
  • 1
    try this reference -> stackoverflow.com/questions/41878838/… Commented Apr 6, 2019 at 16:00
  • It's helps, thank you! Commented Apr 6, 2019 at 16:04

1 Answer 1

5

You can try this:

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

This code is using FormData API

Another option is using form-data package:

const axios = require('axios');
const FormData = require('form-data');

const form = new FormData();
// Second argument  can take Buffer or Stream (lazily read during the request) too.
// Third argument is filename if you want to simulate a file upload. Otherwise omit.
form.append('field', 'a,b,c', 'blah.csv');
axios.post('http://example.org/endpoint', form, {
  headers: form.getHeaders(),
}).then(result => {
  // Handle result…
  console.log(result.data);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.