Node JS 如何 POST 将图像连同请求数据一起发送给另一个 server/api
Node JS How to POST image along with request data to another server/api
我正在尝试 POST 从我的 Node JS 应用程序到另一个 REST API 的图像。我在 Mongo DB 中有图像(作为二进制数组数据),由 Node JS 读取,然后应该 POSTed 到另一个 API。
我面临的问题是如何将请求数据与图像一起发送?我有这个原始数据(即 JSON 格式)应该与图像一起 POSTed:
{"data":{"client":"abc","address": "123"},"meta":{"owner": "yourself","host": "hostishere"}}
我需要使用 'request' 模块来执行此操作。如果有帮助,我可以使用 'multer'。但是,我坚持如何将上述请求数据与图像流一起发送。以下是我当前的代码。你能帮我完成吗?
var options = {
host: 'hostname.com',
port: 80,
path: '/api/content',
method: 'POST',
headers:{
'Content-Type' : 'multipart/form-data'
}
};
var request = http.request(options, function(response) {
var str = '';
var respTime ='';
response.on('data', function (chunk) {
str = str.concat(chunk);
});
response.on('end', () => {
console.log('No more data in response.');
});
setTimeout(function() {
res.send(JSON.stringify(
{
'imageURL': IMG_URL,
'imageId': IMG_ID,
'body': JSON.parse(str)
}
));
}, 1000);
});
request.on('error', (e) => {
console.error('**** problem with request: ', e);
});
request.write(image.IMG_STR); //image.IMG_STR is the binary array representation of the image.
request.end();
更新:2017 年 6 月 6 日
所以,我偶然和提供端点的REST团队交谈,发现数据应该按照以下特定格式发送。下面是成功请求的快照。有人可以帮我处理我应该使用的节点代码吗?我试过 form-data 包,但遇到了同样的错误:
如果您也可以控制 "the other API",您可以将图像作为二进制数据的 base64 表示包含在 post-body 中(并在 API 端对其进行解码)
2017 年 6 月 6 日更新的回答:
根据屏幕截图,API 需要 multipart/formdata。
"request" 模块的此类请求记录在 https://github.com/request/request#multipartform-data-multipart-form-uploads
中
快速示例(未测试):
var formData = {
Data: {data: {client: "abc" ...},
file: fs.createReadStream('testImage_2.jpg'),
};
request.post({url:'<YourUrl>', formData: formData}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
如果您将 body
添加到包含 JSON 数据的请求中,您应该可以发送它:
var options = {
host: 'hostname.com',
port: 80,
path: '/api/content',
method: 'POST',
headers:{
'Content-Type' : 'multipart/form-data'
},
body: {
"data": {"client":"abc","address": "123"},
"meta":{"owner": "yourself","host": "hostishere"}
}
};
我不明白的是为什么你有一个 setTimeout
和 res.send
而没有在任何地方定义 res
变量。
我正在尝试 POST 从我的 Node JS 应用程序到另一个 REST API 的图像。我在 Mongo DB 中有图像(作为二进制数组数据),由 Node JS 读取,然后应该 POSTed 到另一个 API。
我面临的问题是如何将请求数据与图像一起发送?我有这个原始数据(即 JSON 格式)应该与图像一起 POSTed:
{"data":{"client":"abc","address": "123"},"meta":{"owner": "yourself","host": "hostishere"}}
我需要使用 'request' 模块来执行此操作。如果有帮助,我可以使用 'multer'。但是,我坚持如何将上述请求数据与图像流一起发送。以下是我当前的代码。你能帮我完成吗?
var options = {
host: 'hostname.com',
port: 80,
path: '/api/content',
method: 'POST',
headers:{
'Content-Type' : 'multipart/form-data'
}
};
var request = http.request(options, function(response) {
var str = '';
var respTime ='';
response.on('data', function (chunk) {
str = str.concat(chunk);
});
response.on('end', () => {
console.log('No more data in response.');
});
setTimeout(function() {
res.send(JSON.stringify(
{
'imageURL': IMG_URL,
'imageId': IMG_ID,
'body': JSON.parse(str)
}
));
}, 1000);
});
request.on('error', (e) => {
console.error('**** problem with request: ', e);
});
request.write(image.IMG_STR); //image.IMG_STR is the binary array representation of the image.
request.end();
更新:2017 年 6 月 6 日
所以,我偶然和提供端点的REST团队交谈,发现数据应该按照以下特定格式发送。下面是成功请求的快照。有人可以帮我处理我应该使用的节点代码吗?我试过 form-data 包,但遇到了同样的错误:
如果您也可以控制 "the other API",您可以将图像作为二进制数据的 base64 表示包含在 post-body 中(并在 API 端对其进行解码)
2017 年 6 月 6 日更新的回答:
根据屏幕截图,API 需要 multipart/formdata。 "request" 模块的此类请求记录在 https://github.com/request/request#multipartform-data-multipart-form-uploads
中快速示例(未测试):
var formData = {
Data: {data: {client: "abc" ...},
file: fs.createReadStream('testImage_2.jpg'),
};
request.post({url:'<YourUrl>', formData: formData}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
如果您将 body
添加到包含 JSON 数据的请求中,您应该可以发送它:
var options = {
host: 'hostname.com',
port: 80,
path: '/api/content',
method: 'POST',
headers:{
'Content-Type' : 'multipart/form-data'
},
body: {
"data": {"client":"abc","address": "123"},
"meta":{"owner": "yourself","host": "hostishere"}
}
};
我不明白的是为什么你有一个 setTimeout
和 res.send
而没有在任何地方定义 res
变量。