For most of you reading this, it is portably the most basic question and done within 2 minutes.. Maybe someone got the time to provide the code for me or can recommend a resource where this is being explained for an absolute beginner.
I want to call an API from IBM cloud functions that requires authentication.
I got this code from an IBM video tutorial with that I can call any open API:
let rp = require('request-promise')
function main(params) {
if (params.actionA == 'joke') {
const options = {
uri: "http://api.icndb.com/jokes/random",
json: true
}
return rp(options)
.then(res => {
return { response: res }
})
} else if (params.actionB == 'fact') {
const options = {
uri: "https://catfact.ninja/fact",
json: true
}
return rp(options)
.then(res => {
return { response: res }
})
}
}
I want to keep the joke API but want to exchange the Cat fact API with this inspirational quote API (which needs authenticaion): https://english.api.rakuten.net/HealThruWords/api/universal-inspirational-quotes/details
I can get this node.js code from rakuten to call the quote api.
var http = require("https");
var options = {
"method": "GET",
"hostname": "healthruwords.p.rapidapi.com",
"port": null,
"path": "/v1/quotes/?id=731&t=Wisdom&maxR=1&size=medium",
"headers": {
"x-rapidapi-host": "healthruwords.p.rapidapi.com",
"x-rapidapi-key": "api key here",
"useQueryString": true
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.end();
How can I incorporate it into the if function? I want to use that function with watson assistant , which works well with the current code. I just need the catfact api exchanged by the quote api.