最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google apps script - Fetch - Missing required parameter with EasyPost API - Stack Overflow

programmeradmin4浏览0评论

I'm trying to call EasyPost's api in google apps script. I'm following their getting started guide and am attempting to create a shipment. It's appears that I'm able to authenticate correctly, but for some reason I'm getting the following error and I have not been able to figure out why:

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

I don't understand why the server thinks the shipment field is blank. Here is my code:

function myFunction() {
  const api_key = 'TEST_API_KEY';
  const url = '';

  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": "[email protected]"
      },
      "from_address": {
        "name": "EasyPost",
        "street1": "417 Montgomery Street",
        "street2": "5th Floor",
        "city": "San Francisco",
        "state": "CA",
        "zip": "94104",
        "country": "US",
        "phone": "4153334445",
        "email": "[email protected]"
      },
      "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);
};

Appreciate any assistance.

I'm trying to call EasyPost's api in google apps script. I'm following their getting started guide and am attempting to create a shipment. It's appears that I'm able to authenticate correctly, but for some reason I'm getting the following error and I have not been able to figure out why:

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

I don't understand why the server thinks the shipment field is blank. Here is my code:

function myFunction() {
  const api_key = 'TEST_API_KEY';
  const url = 'https://api.easypost/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": "[email protected]"
      },
      "from_address": {
        "name": "EasyPost",
        "street1": "417 Montgomery Street",
        "street2": "5th Floor",
        "city": "San Francisco",
        "state": "CA",
        "zip": "94104",
        "country": "US",
        "phone": "4153334445",
        "email": "[email protected]"
      },
      "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);
};

Appreciate any assistance.

Share Improve this question edited Mar 18 at 2:04 TheMaster 51.2k7 gold badges73 silver badges100 bronze badges asked Mar 18 at 1:48 Sam WBSam WB 1934 gold badges7 silver badges16 bronze badges 3
  • 1 The method fetch of Class UrlFetchApp has no property of body in 2nd argument. Ref So, please modify body: JSON.stringify(data) to payload: JSON.stringify(data) and test it again. – Tanaike Commented Mar 18 at 3:41
  • 1 @Tanaike well now I feel just a bit stupid, lol. Modifying to payload did indeed fix the problem, thank you. – Sam WB Commented Mar 18 at 3:47
  • Thank you for replying. I'm glad your issue was resolved. When your issue is resolved, can you post it as an answer? By this, it will be useful for other users who have the same issue. – Tanaike Commented Mar 18 at 3:48
Add a comment  | 

1 Answer 1

Reset to default 0

UrlFetchApp.fetch does not have a body property, instead I needed to use payload

body: JSON.stringify(data)

modified to

payload: JSON.stringify(data)

resolved the issue.

发布评论

评论列表(0)

  1. 暂无评论