[英]How to set MIME type for POST - multipart/form-data in axios?
I need to send POST request with MIME - multipart/form-data
我需要发送带有MIME的POST请求
multipart/form-data
This is my default configuration for POST headers: axios.defaults.headers.post['Content-Type'] = 'multipart/form-data';
这是我对POST标头的默认配置:
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
我希望默认的
Content-Type
应该是multipart/form-dat
,但是在chrome devtools中,我看到Content-Type: application/json
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 这段代码使用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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.