You need to enable JavaScript to run this app. Google App Script执行GraphQL Mutation报错POST body缺失如何解决
优惠活动
大模型
产品
解决方案
定价
更多

Google App Script执行GraphQL Mutation报错POST body缺失如何解决

Google App Script调用Wave Accounting GraphQL Mutation报错解决方案

问题根因

  • 不需要集成body-parser中间件,body-parser是Node.js后端服务用来解析请求体的组件,你作为客户端发起请求不需要用到该组件,报错是Wave API侧返回的,代表它没有正确解析到你发送的JSON格式请求体。
  • 现有代码存在两个错误:
    1. contentType和muteHttpExceptions是Google App Script UrlFetchApp的顶层配置参数,不应该放在headers对象内
    2. payload直接传入JS对象时,Google App Script会默认将其编码为application/x-www-form-urlencoded格式,即使设置了JSON请求头也不生效,需要手动将请求体序列化为JSON字符串

修正后的完整代码

function Run_Mutation(){
  var token = "Bearer [Censored]"
  var Business_ID = "[Censored]"
  var url = "https://gql.waveapps.com/graphql/public";
  
  var query = 
 "mutation Mutation($input: CustomerCreateInput!) {\
  customerCreate(input: $input) {\
    didSucceed\
  }
}";
  
  var variables = {
    "input": {
      "businessId": Business_ID,
      "name": "Santa",
      "firstName": "Saint",
      "lastName": "Nicholas",
      "email": "santa@example.com",
      "address": {
        "city": "North Pole",
        "postalCode": "H0H 0H0",
        "provinceCode": "CA-NU",
        "countryCode": "CA"
      },
      "currency": "CAD"
    }
  };
  
  // headers仅存放标准请求头字段
  var headers = {
    'Authorization': token
  }
  
  var data = {
    "operationName": "Mutation",
    "query" : query,
    "variables" : variables
  }
  
  // 修正配置位置 + 手动序列化请求体
  var options = {
    'method' : 'POST',
    'headers' : headers,
    'contentType' : 'application/json',
    'muteHttpExceptions' : true,
    'payload' : JSON.stringify(data)
  }
  
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
}

验证说明

修改后重新运行脚本即可正常发起Mutation请求,接口会返回didSucceed为true的成功结果,对应客户信息会同步到你的Wave Accounting账户中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.20 23:36:11