[英]JavaScript Fetch Code Works in Postman, but not Google Script Editor
This JavaScript Fetch code returns the data I need in Postman.这个 JavaScript 获取代码返回 Postman 中我需要的数据。 But, I am trying to get this code to work in Google Script Editor for my google sheet, and it is giving me the following error:
但是,我试图让这段代码在我的谷歌表的谷歌脚本编辑器中工作,它给了我以下错误:
ReferenceError: Headers is not defined line 3. ReferenceError:标题未定义第 3 行。
I am brand new to coding, so please bear with me.我是编码新手,所以请多多包涵。
Any idea what's wrong here?知道这里有什么问题吗?
POSTMAN CODE: POSTMAN 代码:
var myHeaders = new Headers();
myHeaders.append("Content-Type", "text/plain");
var raw = "{\n \"id\": 1,\n \"jsonrpc\": \"1.0\",\n \"method\": \"getbeaconbeststatedetail\",\n \"params\": []\n}";
var requestOptions = {
method: 'GET',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://mainnet.incognito.org/fullnode", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
EDIT:编辑:
I have tried to update this:我试图更新这个:
function myFunction() {
var data = {
'request': {
'id': '1',
'jsonrpc': '1.0',
'method': 'getbeaconbeststatedetail',
'params': []
}
};
var payload = JSON.stringify(data);
var options = {
'method' : 'GET',
'headers': { 'Content-Type': "text/plain", 'Accept': "text/plain"},
'muteHttpExceptions': true,
'contentType' : 'application/json',
'body' : payload
};
var url = "https://mainnet.incognito.org/fullnode";
var response = UrlFetchApp.fetch(url, options);
var txt= response.getContentText();
var d=JSON.parse(txt);
var sh1=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet14");
for (let i = 0; i < d.length; i++) {
sh1.getRange(1, 1).setValue(d);
}
}
But, I am still getting the following error:但是,我仍然收到以下错误:
Exception: Request failed for https://mainnet.incognito.org returned code 400异常:对https://mainnet.incognito.org的请求失败,返回代码 400
If you are trying to make an HTTP request from Google Apps Script, you have to use the UrlFetchApp()
method.如果您尝试从 Google Apps 脚本发出 HTTP 请求,则必须使用
UrlFetchApp()
方法。
As @Tanaike points out, GAS is based in JS but not all JS functions are allowed.正如@Tanaike 指出的那样,GAS 基于 JS,但并非所有 JS 函数都被允许。 However GAS does have many services to integrate with G Suite products, and external APIs or services.
然而,GAS 确实有许多服务可以与 G Suite 产品以及外部 API 或服务集成。 One very useful service is URL Fetch Service which allows you to perform any type of Http request.
一项非常有用的服务是URL Fetch Service ,它允许您执行任何类型的 Http 请求。
// Make a GET request with headers.
var options = {
"method" : "GET",
"headers" : {
"Content-Type" : "text/plain",
"API_KEY": "dummydummy"
}
};
var response = UrlFetchApp.fetch("https://api.myawesomeapi.com/get", options);
You can also make POST or other requests.您还可以发出 POST 或其他请求。 Please refer to the examples in the documentation for use cases.
有关用例,请参阅文档中的示例。
You are receiving the 400 "Bad request" error from the "fullnode" endpoint because it requires a POST request with a payload.您从“fullnode”端点收到 400“Bad request”错误,因为它需要带有有效负载的 POST 请求。
function fetchFullNode() {
var url = "https://mainnet.incognito.org/fullnode";
var data = {
"jsonrpc":"1.0",
"method":"getblockchaininfo",
"params":"",
"id":65
};
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(data)
};
var response = UrlFetchApp.fetch(url, options);
var txt= response.getContentText();
return JSON.parse(txt);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.