3

I used Google Apps Script with

var response = UrlFetchApp.fetch(url, params);

to get a response from an api. Sadly its too many request and too many data i need to be handled with Google Apps Script in Drive

My idea was now to switch to Google cloud functions and request but its not working.

const request = require('request');

const headers = {
    "Authorization" : "Basic " + Buffer.from('blabla').toString('base64')
};

const params = {
    "method":"GET",
    "headers":headers
};

exports.getCheckfrontBookings = (req, res) => {
  let url = 'https://fpronline.checkfront.com/api/3.0/item'

  request({url:url, qs:params}, function(err, response, body) {
    if(err) { console.log(err); return; }
    console.log("Get response: " + response.statusCode);
  });
6
  • You can not return a value from a callback like that. Commented Jan 20, 2019 at 9:54
  • Possible duplicate of How do I return the response from an asynchronous call? Commented Jan 20, 2019 at 9:54
  • ok even without the return. the require('request') doesnt work in Google cloud functions? Commented Jan 20, 2019 at 10:10
  • 1
    What does "doesnt work" mean in detail? Do you see error messages? Commented Jan 20, 2019 at 10:33
  • Detailed stack trace: Error: Cannot find module 'request' Commented Jan 20, 2019 at 11:57

1 Answer 1

2

request supports callback interfaces natively but does not return a promise, which is what you must do within a Cloud Function.

You could use request-promise (https://github.com/request/request-promise) and the rp(...) method which "returns a regular Promises/A+ compliant promise" and then do something like:

const rp = require('request-promise');

exports.getCheckfrontBookings = (req, res) => {

  var options = {
    uri: 'https://fpronline.checkfront.com/api/3.0/item',
    headers: {
      Authorization: 'Basic ' + Buffer.from('blabla').toString('base64')
    },
    json: true // Automatically parses the JSON string in the response
  };

  rp(options)
    .then(response => {
      console.log('Get response: ' + response.statusCode);
      res.send('Success');
    })
    .catch(err => {
      // API call failed...
      res.status(500).send('Error': err);
    });
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.