I've been trying to use an external API to populate a Google Sheet with shipping prices from an Australian API.
When posting the request, Appscript seems to be changing the .com.au
part of the domain to <?>
.
I'm getting a 401 error like so (you can see the .<?>
):
Request failed for https://www.transdirect.<?>/api/quotes returned code 401. Truncated server response: {"message":"You are not permitted to access this area.","code":401,"success":false} (use muteHttpExceptions option to examine full response) (line 36, file "TransDirect API Handler")
Anyone know how I can stop this from happening in my request? Here is the code (I've removed the API key.) :
function Quote() {
var url= "https://www.transdirect.com.au/api/quotes";
var payload = {'declared_value': '100.00',
'items': [
{
'weight': '38.63',
'height': '0.25',
'width': '1.65',
'length': '3.32',
'quantity': 1,
'description': 'carton'
},
{
'weight': '39.63',
'height': '1.25',
'width': '2.65',
'length': '4.32',
'quantity': 2,
'description': 'carton'
}
],
'sender': {
'postcode': '3',
'suburb': 'SYDNEY',
'type': 'business',
'country': 'AU'
},
'receiver': {
'postcode': '3000',
'suburb': 'MELBOURNE',
'type': 'business',
'country': 'AU'
}};
var response = UrlFetchApp.fetch(
url,
{
'method' : 'post',
'contentType': 'application/json',
'Api-Key': '<API KEY HERE>',
'payload' : JSON.stringify(payload),
}
);
var responseCode = response.getResponseCode()
var responseBody = response.getContentText()
if (responseCode === 200) {
var responseJson = JSON.parse(responseBody)
} else {
Logger.log(Utilities.formatString("Request failed. Expected 200, got %d: %s", responseCode, responseBody)
);
}
};
Would you say this is a bug in UrlFetchApp or am I doing something wrong?
Just to clarify, I've tested the API-Key on apiary.io with no dramas (it works).
Thanks, Jake
UrlFetch
is performing the request. You are getting an error from the REST interface because probably your payload is missing the api key inheaders
.I've not tested it, but I'm quite confident it works.