我的Node.js应用程序具有以下端点:
app.post('/uploadimage', function (req, res) {
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
if (err){
console.log(err);
res.send("File(s) upload error");
return;
}
if (files && files.uploadimage){
var imgArray = files.uploadimage;
for (var index = 0; index < imgArray.length; index++) {
var newPath = '/path/to/my/files/';
var img = imgArray[index];
newPath += img.originalFilename;
readAndWriteFile(img, newPath);
}
res.send("File(s) uploaded");
}
else {
res.status(400).send("No 'uploadimage' present in upload request");
}
});
});
当我通过Postman中的表单数据请求将图像直接发布到它时,它可以正常工作。 / uploadimage请求中的标头如下所示:
{ host: 'localhost:7777',
connection: 'keep-alive',
'content-length': '1314170',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
'cache-control': 'no-cache',
origin: 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop',
'postman-token': 'd83b600b-9739-1433-801d-.....',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryhOg3mqey3UTHR3G0',
accept: '*/*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8' }
但是,当我通过如下所示的/ forwardpost端点转发相同的请求时:
app.post('/forwardpost', function (req, res) {
const options = {
url: 'https://localhost:7777/uploadimage',
method: 'POST',
headers: req.headers
};
// act as proxy for POST request, returning result through here
request(options, function(err, response, body) {
// ... handle response here ...
});
});
它不起作用,标题看起来像这样(内容长度现在为零):
{ host: 'localhost:7777',
connection: 'keep-alive',
'content-length': '0',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
'cache-control': 'no-cache',
origin: 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop',
'postman-token': 'c1612b11-be20-47c4-716a-.....',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryhdDKQwuCTXP9JCjj',
accept: '*/*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-GB,en-US;q=0.9,en;q=0.8' }
我尝试手动将content-length设置为已知值,但这不是问题。谁能建议我如何将图像文件实际传递到/ uploadimage端点?
编辑:包括一个屏幕截图,以显示我如何在Postman中执行请求