Reputation: 95
How to Fetch the data from api(https://jsonplaceholder.typicode.com/posts/1) and to show in Actions using fulfillment?
my code:
function apiCall(agent){
https.get('https://jsonplaceholder.typicode.com/posts/1', (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log(JSON.parse(data).explanation);
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
and I'm getting an error:
in functions: getaddrinfo ENOTFOUND jsonplaceholder.typicode.com jsonplaceholder.typicode.com:443
Upvotes: 0
Views: 808
Reputation: 381
There is a Developer Relations Quote sample hosted on the Actions on Google GitHub that demonstrates how to make an external API call from your fulfillment webhooks.
As mentioned in the README of the sample, if you are using Cloud Functions for Firebase to deploy your fulfillment, you need to upgrade your plan, as the free tier of Firebase does not support making outbound network calls.
Upvotes: 1
Reputation: 339
What do you want to receive in this page? I copied your code and changed
console.log(JSON.parse(data).explanation);
to
console.log(data);
And it returns me this:
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
which is why I get when I click your link.
I had no error with your code except that the field 'explanation' does not exist for me.
As I do not get what the message is saying, does your site require some authentication or something ?
Upvotes: 0
Reputation: 846
I have tested API server its works well LINK
Causes
I had the same issue trying to access API server from Cloud functions. That time Billing wasn't the fix as I had billing enabled already.
Solutions
Google Cloud function don't allow you to access outbound internet access without Billing.You just have to enable billing for your project by giving your ATM details don't worry it's free tier.
You can use Google Cloud Function Simulator + any localtunnel
like ngrok or serveo.net then add that link to your fulfillment webhook url
its good solution for development purpose.
Upvotes: 3