3

I am requesting a Sponsered Product report from Amazon's Advertising API. When I send the POST i receive the reportID. I enter the reportID as part of the GET call's path to retrieve the document. I observe the response of type 20, however the content of the response is binary code(I think).

The documentation has indicated I should receive a JSON response but that is not what I am getting back. How do i return the document in the appropriate format?

I have included an image for reference of the response.

Postman Response

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
JaredGibb
  • 83
  • 7

2 Answers2

6

As @tector says, you need to unzip this file. Amazon's documentation is pretty poor and is often mixed with updated and outdated information.

GET /v2/reports/{reportId} does return a JSON response that contains a download URI when the status is 'SUCCESS'.

GET /v2/reports/{reportId}/download, when successful, responds with a 307 redirect to the file that is generated. You are seeing a binary response because it is a file.

In python you would handle the response like this:

import requests
import gzip
import io

headers = {
    "Authorization": f"Bearer {access_code}",
    "Amazon-Advertising-API-Scope": profile_id,
    "Amazon-Advertising-API-ClientId": client_id
}

response = requests.get(location, headers=headers, allow_redirects=True)
if response.ok:
    compressed_file = io.BytesIO(response.content)
    decompressed_file = gzip.GzipFile(fileobj=compressed_file)  # you can shorten the past 2 lines
    final = pd.read_json(decompressed_file)  # put contents into pandas dataframe
mr_mooo_cow
  • 1,098
  • 1
  • 6
  • 16
2

Yeah, that is very confusing. In fact it is a gzipped file that you have to download and unzip:

Screenshot

tector
  • 977
  • 1
  • 8
  • 31