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

使用Google Apps Script调用EasyPost API创建运单遇必填参数缺失错误

问题:调用EasyPost API创建运单时出现422错误(shipment字段不能为空)

在Google Apps Script中调用EasyPost API创建运单,认证已成功,但收到422错误,提示shipment字段不能为空,错误信息如下:

Exception: Request failed for https://api.easypost.com returned code 422. Truncated server response: {"error":{"code":"PARAMETER.REQUIRED","message":"Missing required parameter.","errors":[{"field":"shipment","message":"cannot be blank"}]}}

附上相关代码:

function myFunction() {
  const api_key = 'TEST_API_KEY';
  const url = 'https://api.easypost.com/v2/shipments';

  const data = {
    "shipment": {
      "to_address": {
        "name": "Dr. Steve Brule",
        "street1": "179 N Harbor Dr",
        "city": "Redondo Beach",
        "state": "CA",
        "zip": "90277",
        "country": "US",
        "phone": "8573875756",
        "email": "dr_steve_brule@gmail.com"
      },
      "from_address": {
        "name": "EasyPost",
        "street1": "417 Montgomery Street",
        "street2": "5th Floor",
        "city": "San Francisco",
        "state": "CA",
        "zip": "94104",
        "country": "US",
        "phone": "4153334445",
        "email": "support@easypost.com"
      },
      "parcel": {
        "length": "20.2",
        "width": "10.9",
        "height": "5",
        "weight": "65.9"
      },
    }
  };

  const options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Basic ' + Utilities.base64Encode(api_key)
    },
    body: JSON.stringify(data)
  };

  const results = UrlFetchApp.fetch(url, options);
  console.log(results);
};

解决方案

出现这个错误的核心原因有两个:

1. Basic认证格式错误

EasyPost的API要求Basic认证格式为API Key + 冒号(密码留空)后进行Base64编码。原代码中仅对API Key进行编码,缺少冒号,导致服务器无法正确解析认证信息,进而无法识别请求体中的shipment参数。

2. JSON对象存在多余逗号

data.shipment.parcel对象末尾的多余逗号虽然不会导致JS报错,但可能在序列化时引发潜在问题,建议移除。

修改后的代码如下:

function myFunction() {
  const api_key = 'TEST_API_KEY';
  const url = 'https://api.easypost.com/v2/shipments';

  const data = {
    "shipment": {
      "to_address": {
        "name": "Dr. Steve Brule",
        "street1": "179 N Harbor Dr",
        "city": "Redondo Beach",
        "state": "CA",
        "zip": "90277",
        "country": "US",
        "phone": "8573875756",
        "email": "dr_steve_brule@gmail.com"
      },
      "from_address": {
        "name": "EasyPost",
        "street1": "417 Montgomery Street",
        "street2": "5th Floor",
        "city": "San Francisco",
        "state": "CA",
        "zip": "94104",
        "country": "US",
        "phone": "4153334445",
        "email": "support@easypost.com"
      },
      "parcel": {
        "length": "20.2",
        "width": "10.9",
        "height": "5",
        "weight": "65.9"
      }
    }
  };

  const options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Basic ' + Utilities.base64Encode(api_key + ':')
    },
    body: JSON.stringify(data)
  };

  const results = UrlFetchApp.fetch(url, options);
  console.log(results);
};

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 18:33:20