'Apps Script - UrlFetchApp.fetch {url, method: "GET"} to a gzip gets failed with code 406

I'm on a multi months quest here. Any help would be much appreciated!

I'm trying to connect via API on App Store Connect using UrlFetchApp.fetch(). After having struggled in the desert to generate the correct JWT for authentification on GAS, I now face harsh reality: the server answer (content-type and content-encoding) is not JSON but GZIP.

Code 406 message: "Truncated server response: The provided Accept header is not supported for this request. Requested: application/json Allowed: application/a-gzip"

Is there a way to still get access to the file?

Here is the part of the code doing the call only (JWT signature code for authentification is above -> "sJWT")

var url = "https://api.appstoreconnect.apple.com/v1/financeReports?filter[regionCode]=ZZ&filter[reportDate]=2019-11&filter[reportType]=FINANCIAL&filter[vendorNumber]=xxx"

var response = UrlFetchApp.fetch(url, { method : "GET", headers : { "Authorization" : "Bearer "+sJWT }});

Many thanks!



Solution 1:[1]

How about this answer? Please think of this as just one of several possible answers.

Modification points:

  • When I saw the official document, it is required to use application/a-gzip for Accept in the request headers.
  • And also, in this case, the response returns the content of gzip. So it is required to decompress the content.

When above points are reflected to your script, how about the following modification?

Modified script:

var url = "https://api.appstoreconnect.apple.com/v1/financeReports?filter[regionCode]=ZZ&filter[reportDate]=2019-11&filter[reportType]=FINANCIAL&filter[vendorNumber]=xxx"
var response = UrlFetchApp.fetch(url, {
  method: "GET",
  headers: {
    "Authorization": "Bearer " + sJWT,
    "Accept": "application/a-gzip"  // Added
  }
});
var res = Utilities.ungzip(response.getBlob());  // Added

Note:

  • Above modified script supposes that the values of your URL and sJWT are correct for using the API.

References:

Unfortunately, I cannot test above script. I apologize for this. So if above modified script didn't resolve your issue, I apologize.

Solution 2:[2]

Try changing the blob content type to "application/x-gzip":

var url = "https://api.appstoreconnect.apple.com/v1/financeReports?filter[regionCode]=ZZ&filter[reportDate]=2019-11&filter[reportType]=FINANCIAL&filter[vendorNumber]=xxx"
var response = UrlFetchApp.fetch(url, {
  method: "GET",
  headers: {
    "Authorization": "Bearer " + sJWT,
    "Accept": "application/a-gzip"
  }
});
var res = Utilities.ungzip(response.getBlob().setContentType("application/x-gzip"));  // Changed

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 triskybro