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 Answer
Reset to default 0UrlFetchApp.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.
fetch
of Class UrlFetchApp has no property ofbody
in 2nd argument. Ref So, please modifybody: JSON.stringify(data)
topayload: JSON.stringify(data)
and test it again. – Tanaike Commented Mar 18 at 3:41payload
did indeed fix the problem, thank you. – Sam WB Commented Mar 18 at 3:47