vanquish vanquish - 4 months ago 19
Javascript Question

Javascript - How to make REST API Calls

I am trying to access Firebase Database using REST API. Using cURL i am able to retrive data. I am unable to do the same in javascript.

cURL

curl 'https://testproject123.firebaseio.com/subscription.json?auth=aabbccddeeff123'


Javascript

var request = require('request');

var options = {
url: 'https://testproject123.firebaseio.com/subscription.json?auth=aabbccddeeff123' };

function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
} }

request(options, callback);


Note: The above code i am trying to run on Bluemix/OpenWhisk.
Kindly let me know how to fix this.

Answer

The structure of your action is a bit off for OpenWhisk. Javascript actions need to have a main method and you'll have to use whisk.async() and whisk.done() to make your (asynchronous) REST API call work as you'd expect it.

An example of how to call an external API using a Javascript action in OpenWhisk can be found here.

Comments