在做了一点尝试之后,我得到了以下代码,它对我来说工作得很好。我使用了Multer中间件(
https://github.com/expressjs/multer
)对于原始多部分上载。令人感兴趣的是,除非您指定文件名选项,否则请求似乎无法很好地上传文件。
const multer = require('multer');
const upload = multer();
app.post('/', upload.any(), (req, res) => {
const url = 'https://api-us.faceplusplus.com/facepp/v3/detect';
console.log('Image upload complete, creating request to: ' + url);
var formData = {
api_key: process.env.FACEPP_API_KEY,
api_secret: process.env.FACEPP_API_SECRET,
image_file: {
value: req.files[0].buffer, // Upload the first file in the multi-part post
options: {
filename: 'image_file'
}
}
};
const options = {
uri: url,
formData: formData,
method: 'POST'
}
request(options, (err, response, body) => {
console.log('Request complete');
if (err) console.log('Request err: ', err);
console.log(body)
})
})
我得到的回应如下:
{
"image_id": "GuF0MUPoaTcL/rbbcg+2kA==",
"request_id": "1520789753,d913cce4-118e-4893-a1ee-d1ace2b6a65b",
"time_used": 142,
"faces": [{
"face_rectangle": {
"width": 183,
"top": 125,
"left": 220,
"height": 183
},
"face_token": "8b8e327edfc10730f344b1465934a478"
}]
}
我使用curl测试了图像上传到本地服务器的情况,如下所示:
curl -v -i -F "data=@smiling_woman.jpg" -H "Content-Type: multipart/form-data" -X POST http://localhost:3000/