You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将可正常运行的CURL/XHR请求改写为适配Google Apps Script的代码

修复后的可运行代码

function callPolygonRPC() {
  const rpcUrl = "https://rpc-mainnet.maticvigil.com/";
  const payload = {
    jsonrpc: "2.0",
    id: "dontcare",
    method: "eth_call",
    params: [
      {
        from: "0x0000000000000000000000000000000000000000",
        data: "0x7b84daec00000000000000000000000000000000000000000000000000000000000000280000000000000000000000009fd15feb6f86d2da6a3ac17372debd1db4e02e93",
        to: "0xbda1f897e851c7ef22cd490d2cf2dace4645a904"
      },
      "latest"
    ]
  };

  const options = {
    method: "POST",
    contentType: "application/json",
    payload: JSON.stringify(payload),
    muteHttpExceptions: true
  };

  const response = UrlFetchApp.fetch(rpcUrl, options);
  Logger.log("状态码:" + response.getResponseCode());
  Logger.log("返回结果:" + response.getContentText());
}

核心修改点

  • 移除了所有无效冗余配置:mode、credentials、referrerPolicy以及sec-*开头的浏览器专属请求头、referrer字段等,这些参数是浏览器Fetch API的专属配置,Google Apps Script的UrlFetchApp并不支持,多余的请求头还会触发公共RPC节点的安全拦截机制。
  • 不再手动拼接转义JSON字符串,改用JSON.stringify()自动生成请求体,避免转义错误导致JSON结构异常,触发节点返回Unrecognized JSONRPC command错误。
  • 增加muteHttpExceptions: true配置,请求返回非200状态码时不会直接抛出异常,方便拿到完整返回内容排查问题。
  • 直接用UrlFetchApp内置的contentType字段配置请求类型,不需要手动在headers里加Content-Type。

如果修改后仍然返回403,说明当前公共RPC节点拦截了Google Apps Script的出口IP,更换其他可用的Polygon主网公共RPC节点即可。

内容的提问来源于stack exchange,提问作者Kibou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 04:51:02