[英]sending file and json in POST multipart/form-data request with axios
I am trying to send a file and some json in the same multipart POST request to my REST endpoint.我正在尝试在同一个多部分 POST 请求中向我的 REST 端点发送一个文件和一些 json。 The request is made directly from javascript using axios library as shown in the method below.该请求是使用 axios 库直接从 javascript 发出的,如下面的方法所示。
doAjaxPost() {
var formData = new FormData();
var file = document.querySelector('#file');
formData.append("file", file.files[0]);
formData.append("document", documentJson);
axios({
method: 'post',
url: 'http://192.168.1.69:8080/api/files',
data: formData,
})
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});
}
However, the problem is when I inspect the request in chrome developer tools in the network tab, I find no Content-Type
field for document
, while for file
field Content-Type
is application/pdf
(I'm sending a pdf file).但是,问题是当我在网络选项卡中的 chrome 开发人员工具中检查请求时,我没有找到document
Content-Type
字段,而file
字段Content-Type
是application/pdf
(我正在发送 pdf 文件)。
On the server Content-Type
for document
is text/plain;charset=us-ascii
.在服务器上, document
的Content-Type
是text/plain;charset=us-ascii
。
Update:更新:
I managed to make a correct request via Postman, by sending document
as a .json
file.通过将document
作为.json
文件发送,我设法通过 Postman 提出了正确的请求。 Though I discovered this only works on Linux/Mac.虽然我发现这只适用于 Linux/Mac。
To set a content-type you need to pass a file-like object.要设置内容类型,您需要传递一个类似文件的对象。 You can create one using a Blob
.您可以使用Blob
创建一个。
const obj = {
hello: "world"
};
const json = JSON.stringify(obj);
const blob = new Blob([json], {
type: 'application/json'
});
const data = new FormData();
data.append("document", blob);
axios({
method: 'post',
url: '/sample',
data: data,
})
Try this.尝试这个。
doAjaxPost() {
var formData = new FormData();
var file = document.querySelector('#file');
formData.append("file", file.files[0]);
// formData.append("document", documentJson); instead of this, use the line below.
formData.append("document", JSON.stringify(documentJson));
axios({
method: 'post',
url: 'http://192.168.1.69:8080/api/files',
data: formData,
})
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});
}
You can decode this stringified JSON in the back-end.您可以在后端解码这个字符串化的 JSON。
you only need to add the right headers to your request您只需要在请求中添加正确的标题
axios({
method: 'post',
url: 'http://192.168.1.69:8080/api/files',
data: formData,
header: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
})
You cant set content-type to documentJson
, because non-file fields must not have a Content-Type
header, see HTML 5 spec 4.10.21.8 multipart form data .您不能将 content-type 设置为documentJson
,因为非文件字段不得具有Content-Type
header,请参阅HTML 5 spec 4.10.21.8 multipart form data 。
And there`s two way to achieve your goals:有两种方法可以实现您的目标:
Content-Type
like this answer below通过类似文件的 object 并设置Content-Type
如下面的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.