What's the server-side equivalent of $.ajax() in Google Apps Scripts?

后端 未结 2 1139
生来不讨喜
生来不讨喜 2021-02-10 15:18

I want to perform an HTTP request from server-side code in a Google App Script with an Authorization header. Is there an App Script API for sending HTTP requests?

相关标签:
2条回答
  • 2021-02-10 15:56

    You can send HTTP requests using the UrlFetchApp object. It has a fetch(url, params) method that returns a HTTPResponse with information about the result of the HTTP fetch.

    function testapi(){
    
        var encode =  Utilities.base64Encode('username:password', Utilities.Charset.UTF_8);
        Logger.log(encode);
    
        var option = {
          headers : {
                Authorization: "Basic "+ encode
          }   
        }
    
        var url = "URL";
        var response = UrlFetchApp.fetch(url, option).getContentText()
        response = JSON.parse(response);
    
        for (var key in response){
          Logger.log(key);
        }
    }
    
    0 讨论(0)
  • 2021-02-10 16:14

    you need to use UrlFetchApp. see the official docs

    0 讨论(0)
提交回复
热议问题