humanstupidity / amazon-advertising-api

This class helps you interfacing the Amazon Advertising API.
MIT License
3 stars 5 forks source link

getReport method returns unicode response #4

Closed Jamal1337 closed 3 years ago

Jamal1337 commented 4 years ago

Hey there, first of all thanks a lot for providing this API to the amazon advertising endpoint. It works fine so far, but after requesting a report and then fetching it, I get the body only in unicode.

So it looks like this: ��Ak�0��>����J�K�x�M�����F)���v0ƾ�_�/�L6�V*�H����H^>�ė�h†Qb��Mw�lM5'�5(2l��䭼�K�C��2�y٨[��E�hj�MR�����B!'T U�� MGI� w>�}�C��i���Hf~�0��0����)0��+�o c ��K 3�g���e�Mh�w��_;�0�$M��^Y���:�:c�ZG � R�Aw抙?ǫQ��-�K�6@�x\$L̀v��J|cFR0��G��w������R� �z��U���v_>,]Qe���!P��qa��{

Not sure how to fix that to be able to read it in json. Maybe you can help?

Thanks!

humanstupidity commented 4 years ago

I will try to fix it.

Thx for your response.

Jamal1337 commented 4 years ago

This would be awesome, I am struggling with this for about 2 weeks and can't get it to work :/

Thanks a lot!

humanstupidity commented 4 years ago

Hello Jamal1337,

could you please debug the function und send me the variable values. Currently I didnt have a developer environment.

Jamal1337 commented 4 years ago

Hey, what kind of variable values do you need?

The requestReport function works flawless and returns a reportID like this: "amzn1.clicksAPI.v1.p4.5E28A20C.781d6a14-57b9-431b-9334-80d215da0917"

then I call the getReport function on this reportID with those headers: { url: 'https://advertising-api-eu.amazon.com/v1/reports/amzn1.clicksAPI.v1.p4.5E28A20C.781d6a14-57b9-431b-9334-80d215da0917/download', headers: { Authorization: 'Bearer <Access_Token>', 'Amazon-Advertising-API-Scope': '<Profile_ID>' }, followRedirect: false, gzip: true }

which tries to download the report with those headers: { url: 'https://amazon-advertising-api-reports-prod-euamazon.s3.amazonaws.com/a1pa6795ukmfr9-a31y9ymevxu2re-report-campaign-cc5835b3-5df4-4a17-a42a-4cad629a2967.json.gz?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20200210T134332Z&X-Amz-SignedHeaders=host&X-Amz-Expires=30&X-Amz-Credential=AKIA3P6LC7ARGHRXKW4R%2F20200210%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=bd9686607b09d83fcc2e061a909d93ead40e2db299b827f10385a58733304fbf', headers: { 'Amazon-Advertising-API-Scope': '<Profile_ID>' }, followRedirect: false, gzip: true }

and the result of this request is the unicode body mentioned above.

I also found a post on reddit with the exact same issue but could not figure it out how to fix it. You can take a look at it here: https://www.reddit.com/r/learnjavascript/comments/d5xdt0/struggling_to_get_response_from_https_get_when/

humanstupidity commented 4 years ago

If your Stackoverflow post is correct you got this as a result: { success: false, code: 200, response: '\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000���j�@\u0010\u0006�{�B<�\u0010�������P�\u0016�[���M�F\r\u001a\u000b����\u0002y�nӖ�!�7����7�O\u001faRT�p�Fa\u0012g�8}�g�p\u0002H\\\u0019�H�����.M^��4ۗ���"�Z�\u0016q��I8��MR:��y3�\u001a/�F�P3\u0004�C�\u0017�t\\\u0017Y\u001a��\u001b��v��Hf��(��0�+֧�g�8��]\u000b\u0019\u0013�\u001d-I\u001b.%��g�����$���\u001f(��f~�tʐ�H�/C���\u0011�>\u0014M\u0000�Fw\f��\u001b\u0018���\f�71.���]Ev9[4\r1��5��!�˥�i\u0018�m�\u001c�R�=3��I�VL(����t�~sm_��\\i!\u0005\n' + '�٠�aU���=��e�\u0007KW�Ypk�z(��Q��\u0003\u0013$�em\u0010�\u0018=�d�����}���y3��\u000b.��=9\u0004\u0000\u0000', requestId: 0 }`

My problem is that success: false and code: 200 cant occurre at the same time. Can you please debug the code flow of the "_executeRequest" function.

The body looks like the beginning of a gzip file. So we have to receive the remaining parts of the gzip file somehow.

Jamal1337 commented 4 years ago

Yeah I think there is a tiny error in the code, as you hardcode the success: false part in line 329: let result={ success: false, code: response.statusCode, response: response.body, requestId: 0 };

so probably this should be just according to the statuscode?

And yeah I am just wondering why this is a gzip file, I thought with the gzip: true flag it automatically decompresses the file. I also tried using the zlib library but only got errors.

Jamal1337 commented 4 years ago

Any news on this? :/

l290347877 commented 3 years ago

Any news on this? :/ I have the same problem, in the requestOptions Increase the attributes with 'encoding : null',the response with type'buffer' It decompresses correctly. But the API itself does not automatically decompress

humanstupidity commented 3 years ago

I am currently working on a new version. The new version will fix the error.