Node.js 使用节点JSON有效负载发布请求
我试图点击对讲机API来检索对话列表,但我不知道出了什么问题。代码如下:Node.js 使用节点JSON有效负载发布请求,node.js,Node.js,我试图点击对讲机API来检索对话列表,但我不知道出了什么问题。代码如下: const request=require('request') const search_intercom=(admin_id, callback) => { const options = { url: 'https://api.intercom.io/conversations/search', method: 'POST', headers: {
const request=require('request')
const search_intercom=(admin_id, callback) => {
const options = {
url: 'https://api.intercom.io/conversations/search',
method: 'POST',
headers: {
Authorization: 'Bearer <token>'
},
json: {
query: JSON.stringify({
"field": "teammate_ids",
"operator": "=",
"value": admin_id
})
}
};
request(options, (error, {body} = {}) => {
if (error) {
callback('unable to connect to intercom API', undefined)
} else if (body.length === 0) {
callback('something went wrong', undefined)
} else {
callback(undefined, {
conversation_id: body.conversations[0].id,
client_name: body.conversations[0].source.author.name
})
console.log(body)
}
})
}
module.exports = search_intercom
以下是身体反应的内容:
{
type: 'error.list',
request_id: '<some request_id>',
errors: [ { code: 'server_error', message: 'Server Error' } ]
}
{
键入:“error.list”,
请求\u id:“”,
错误:[{代码:'server_error',消息:'server error'}]
}
我应该去哪里看?我尝试了几种不同的发送有效载荷的选项,我猜这就是问题所在,但我找不到获胜的公式…看起来我把身体搞错了
选项
应如下所示:
const options = {
url: 'https://api.intercom.io/conversations/search',
method: 'POST',
headers: {
Authorization: 'Bearer <token>'
},
json: true,
body: {
query: {
"field": "teammate_ids",
"operator": "=",
"value": JSON.stringify(admin_id)
}
}
};
const选项={
网址:'https://api.intercom.io/conversations/search',
方法:“POST”,
标题:{
授权:“持票人”
},
是的,
正文:{
查询:{
“场”:“队友ID”,
“运算符”:“=”,
“value”:JSON.stringify(管理员id)
}
}
};
如果主体是一个数组body.length==0
那么conversation\u id:body[0]。conversations[0]。id
是的,这可能是错误的测试。body
绝对不是arrayNote,您不应该再使用请求模块了,它已经被弃用了:我知道这一点。老实说,这很有趣,因为它仍然是执行http请求的下载最多的模块。。。
const options = {
url: 'https://api.intercom.io/conversations/search',
method: 'POST',
headers: {
Authorization: 'Bearer <token>'
},
json: true,
body: {
query: {
"field": "teammate_ids",
"operator": "=",
"value": JSON.stringify(admin_id)
}
}
};