从本质上讲,我需要从我的NodeJS服务将本地映像发布到Cisco Webex机房。对于本地文件,您需要执行multipart/form-data
请求,而不是documentation中提到的JSON。
CURL看起来像
curl --request POST \
--header "Authorization: Bearer ACCESS_TOKEN" \
--form "files=@/home/desktop/example.png;type=image/png" \
--form "roomId=Y2lzY2....." \
--form "text=example attached" \
https://api.ciscospark.com/v1/messages
但是我不确定如何将其转换为nodeJS请求格式。我尝试使用CURL到节点请求转换器here,但似乎不处理multipart/form-data
类型。请提出建议。
编辑:经过研究,我想出了以下代码
var request = require('request');
var fs = require('fs');
var params = { roomId: ROOMID,
text: "hello....",
files: {
value: fs.createReadStream(PATH_WO_FILENAME),
options: {
filename: 'image.jpg',
contentType: 'jpg'
}
}
};
var headersWebex = {
'Authorization': 'Bearer MY_BOT_ACCESS_TOKEN',
'Content-Type': 'multipart/form-data' }
request.post({
headers: headersWebex,
url: 'https://api.ciscospark.com/v1/messages',
method: 'POST',
body: params
}, function(error, response, body){
console.log(body);
});
但是它抛出错误
undefined
_http_outgoing.js:642
throw new TypeError('First argument must be a string or Buffer');
答案 0 :(得分:1)
好的,这就是我的工作方式。我本质上需要更深入地研究@Evan提到的docs
var request = require('request');
var fs = require('fs');
var roomID = 'MY_ROOM_ID'
var params = {
roomId: roomID,
text: "hello....",
files: {
value: fs.createReadStream('./image.jpg'),
options: {
filename: 'image.jpg',
contentType: 'image/jpg'
}
}
};
var headersWebex = {
'Authorization': 'Bearer MY_BOT_ACCESS_TOKEN',
'Content-Type': 'application/json'
}
request.post({
headers: headersWebex,
url: 'https://api.ciscospark.com/v1/messages',
method: 'POST',
formData: params
}, function(error, response, body){
if (error)
console.log(error)
console.log(body);
});