简体   繁体   English

JavaScript 获取代码适用于 Postman,但不适用于 Google 脚本编辑器

[英]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 请求。

For example:例如:

// 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.有关用例,请参阅文档中的示例。

Update:更新:

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.

相关问题 谷歌脚本编辑器 mailchimp - 获取段订阅者 - google script editor mailchimp - fetch segment subscriber 用于隐藏单元格的 Google 电子表格脚本编辑器代码 - Google Spreadsheet Script Editor Code For Hiding Cells Javascript HideRows(Google表格脚本编辑器) - Javascript HideRows (Google Sheet Script Editor) 请求 Google Apps 脚本实现从 Postman 开始工作,但不从 javascript 开始工作 - Request to Google Apps Scripts Implementation works from Postman but not from javascript 在谷歌表格脚本编辑器中看不到脚本代码 - Can't see the script code in google sheets script editor Google 表格脚本 function UrlFetchApp.fetch 不从.onEdit(e) 运行但从编辑器运行 - Google sheets scripts function UrlFetchApp.fetch does not run from .onEdit(e) but works from editor 从 Google 表格的编辑器中隐藏 Apps 脚本代码 - Hide Apps Script code from editor of a Google Sheet 调用insertSheet可以在Google脚本编辑器中使用,并且已获得授权,但从电子表格调用时可以使用 - Call to insertSheet works in Google Script Editor and was authorised but dosent work when calling from the spreadsheet JS 代码适用于其他编辑器,但不适用于 Google Apps 脚本 - JS Code works on other editors but not Google Apps Script Google Script Editor getRange 问题 - Google Script Editor getRange issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM