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

后端 未结 2 1137
生来不讨喜
生来不讨喜 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);
        }
    }
    

提交回复
热议问题