[英]What's the server-side equivalent of $.ajax() in Google Apps Scripts?
I want to perform an HTTP request from server-side code in a Google App Script with an Authorization
header. 我想从带有
Authorization
标头的Google App Script中的服务器端代码执行HTTP请求。 Is there an App Script API for sending HTTP requests? 是否有用于发送HTTP请求的App Script API?
What's the equivalent of this code in a Google Apps Script? 在Google Apps脚本中,此代码的等效内容是什么?
var api = "URL";
$.ajax({
type: 'GET',
url: api,
contentType: 'application/json',
dataType:'json',
data: {},
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', makeBaseAuth('username', 'password'));
}
});
You can send HTTP requests using the UrlFetchApp
object. 您可以使用
UrlFetchApp
对象发送HTTP请求。 It has a fetch(url, params)
method that returns a HTTPResponse
with information about the result of the HTTP fetch. 它有一个
fetch(url, params)
方法,它返回一个HTTPResponse
其中包含有关HTTP提取结果的信息。
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);
}
}
you need to use UrlFetchApp
. 你需要使用
UrlFetchApp
。 see the official docs 看官方文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.