[英]Cannot read POST response body from external api in firebase cloud functions
I have a cloud function which makes an http POST request to the LinkedIn API in order to retrieve an access token.我有一个云 function 向 LinkedIn API 发出 http POST 请求以检索访问令牌。 The problem is that I cannot read the
body
of the response, it's always undefined
.问题是我无法读取响应的
body
,它总是undefined
。
Here's the code这是代码
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
}
});
If I try the same request with Postman or curl it works perfectly, and I cannot really understand why.如果我用 Postman 或 curl 尝试相同的请求,它会完美运行,但我真的不明白为什么。
Before this request, I made another GET request to the linkedin API and it works normally.在此请求之前,我向linkedin API 发出了另一个GET 请求,它工作正常。 Maybe there's a problem with POST requests.
也许 POST 请求有问题。
I think you are missing headers.我认为您缺少标题。
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.