How to fetch a URL with Google Cloud functions? request?

后端 未结 1 1726
陌清茗
陌清茗 2020-12-12 03:06

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

相关标签:
1条回答
  • 2020-12-12 03:54

    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);
        });
    };
    
    0 讨论(0)
提交回复
热议问题