如何发布JSON+;NodeJS中带有Unirest的pdf文件
在NodeJS中,我尝试使用以下代码将JSON数据与文件一起发布到服务器:如何发布JSON+;NodeJS中带有Unirest的pdf文件,json,node.js,unirest,Json,Node.js,Unirest,在NodeJS中,我尝试使用以下代码将JSON数据与文件一起发布到服务器: unirest.post(url) .headers(headers) .send(data) .attach('file', file) .end(function (response) { var statusCode = response.status; if (statusCode != 200) { console.log("Result: ", response.error);
unirest.post(url)
.headers(headers)
.send(data)
.attach('file', file)
.end(function (response) {
var statusCode = response.status;
if (statusCode != 200) {
console.log("Result: ", response.error);
}
});
但是,在服务器上,我只从
.send(data)
接收文件,而不是JSON对象。我看到有一个.multipart()
函数可以使用,但我不确定如何最好地使用它?当您通过http发送JSON数据时,内容类型是application/JSON
。通过http发送文件时,内容类型为multipart/formdata
。您可以在发送多部分请求时发送表单字段,但不能在多部分请求中发送JSON数据。您有两个选项可以发送此请求
多部分/form data
时,将JSON数据字符串化,并将其作为表单字段发送,然后在另一端对其进行解析application/json
时,Base64可以将其作为json数据中的属性进行归档和发送谢谢你的解释