![](/img/trans.png)
[英]Dialogflow + external API + Google Cloud Functions *without* Firebase: how to return fulfillment response?
[英]Cannot read POST response body from external api in firebase cloud functions
我有一个云 function 向 LinkedIn API 发出 http POST 请求以检索访问令牌。 问题是我无法读取响应的body
,它总是undefined
。
这是代码
exports.linkedinToken = functions.https.onRequest((req, res) => {
let url = "https://linkedin.com/oauth/v2/accessToken?...REQUIRED_PARAMS...";
request.post(url, {json: true}, (err, response, body) => {
if (!!err) { // this is never happening
console.log(err);
throw err;
} else {
console.log(response.body); // this is always undefined
console.log(body); // this is always undefined
}
});
如果我用 Postman 或 curl 尝试相同的请求,它会完美运行,但我真的不明白为什么。
在此请求之前,我向linkedin API 发出了另一个GET 请求,它工作正常。 也许 POST 请求有问题。
我认为您缺少标题。
var request = require('request');
request.post({
headers: {
'content-type' : 'application/x-www-form-urlencoded',
'cache-control' : 'no-cache'
},
url: url
}, function(error, response, body){
console.log(body);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.