1

I'm using a third-party API and my POST request works as expected when I send from the API documentation "test" page in my browser, or from the Postman Windows native app.

I get error codes when I send the same request from Google Apps Script. I am able to send/receive GET requests using GAS and I can also send/receive POST requests using GAS to other APIs.

This GAS GET request works correctly:

var options = {
  "headers" : {
    "Authorization" : "Bearer " + accessToken,
    "Accept": "application/json"
   }
 };
var response = UrlFetchApp.fetch(url, options);    

Headers:

Accept: application/json
Accept-Encoding: gzip
Accept-Language: en-US
Authorization: Bearer {token}
Content-Length: 638
Content-Type: application/json
DNT: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML
X-Forwarded-For: xxx.xxx.xxx.xx
X-Forwarded-Port: 443
X-Forwarded-Proto: https

This GAS POST request produces errors:

var options = {
 "headers" : {
   "Authorization" : "Bearer " + accessToken,
   "Accept": "application/json",
   "Content-Type": "application/json",
   "Accept-Language": "en-US"
 },
"method" : "post",
"payload" : JSON.stringify(this_payload)
};
var response = UrlFetchApp.fetch(url, options);

The GAS POST request sends these headers:

Accept: application/json
Accept-Encoding: gzip,deflate,br
Accept-Language:  en-US
Authorization: Bearer {token}
Connection:  close
Content-Length: 387
Content-Type: application/json
User-Agent: Mozilla/5.0 (compatible; Google-Apps-Script)

This GAS POST request gives the error:

Response Code 500
code: infrastructure.common.SystemException
description:"System error, please re-try"

Is it possible that the API is simply not accepting connections from GAS? The User-Agent lists Google-Apps-Script. Or is it more likely that I have some parameter wrong? I am able to get the parameters correct via two other platforms, so I think parameters are correct.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Womprat
  • 133
  • 11
  • Capture the outgoing POST request and headers from postman and diff it against GAS? – TheMaster Jan 08 '19 at 08:16
  • As suggested by TheMaster, I used Postman to send the *working* request to httpbin.org. I then used GAS to send the *non-working* request to httpbin.org. Some of the headers were different, but the problem was not with headers. It was a difference in a randomly generated parameter that apparently didn't meet the API specifications. I am now able to get the same response in GAS as I do in any browser or plugin :-) – Womprat Jan 08 '19 at 18:57

0 Answers0