如何使用node.js调用API,并在主体中包含JSON字符串
How to call an API using node.js and include JSON string in the body?
我想通过http调用一个外部API服务
但是,web服务器需要自定义报头和JSON字符串。
我遇到了一个请求包,我想使用它。
这是我到目前为止所做的,但不确定如何添加json字符串的请求?
var request = require('request')
request({
url: 'http://www.example.com/test',
method: 'POST',
headers: {
{
name: 'content-type',
value: 'application/json'
},
{
name: 'EXTERNAL-API-token',
value: 'some string generated by the API'
},
{
name: 'EXTERNAL-API-session-ID',
value: 'some integer generated by the API'
},
{
name: 'Cookie',
value: 'some value received by the API'
}
}
})
为请求的options对象设置'json'和'body'属性:
var request = require('request')
request({
url: 'http://www.example.com/test',
method: 'POST',
headers: {
{
name: 'content-type',
value: 'application/json'
}, {
name: 'EXTERNAL-API-token',
value: 'some string generated by the API'
}, {
name: 'EXTERNAL-API-session-ID',
value: 'some integer generated by the API'
}, {
name: 'Cookie',
value: 'some value received by the API'
}
},
json: true,
body: {
key: val
}
});
相关文章:
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 货币代码为欧元-金额的格式不应包含小数
- 如何包含特定于每个视图angularjs的javascript文件
- 我如何修复包含在captcha的addthis中的错误
- 正在注入包含JS的HTML
- Json数据包含日期和时间格式
- Angular js-返回一个包含类似
- jQuery/JS包含运算符或类似运算符
- 如果href包含X,请更改href
- 主体上的addEventListener('mousemove',..)-有多糟糕
- JavaScript数组包含一个值
- 带有填充属性的SVG矩形显示在包含元素的上方插入框阴影
- 在Meteor项目中安装并包含npm模块后出错
- 传递包含'%的参数'在URL中
- 对一个对象使用reduce可以返回一个没有't在数组中包含目标字母
- 如何首先设置样式<td>表中包含在窗体中的元素
- 每天用javascript包含不同的php文件
- 数据绑定:'系统Char'不包含名为'xxxxx'
- 如何在包含col元素的主体中选择tr
- 如何使用node.js调用API,并在主体中包含JSON字符串