BookmarkSubscribe
Choose Language Hide Translation Bar
uday_guntupalli
Community Trekker

API Response Encoded as Gzip

All, 

  I am tapping an internal API and the expected response of the API endpoint is an Excel File. However, when I try to  use HTTP request, I receive a response. I am assuming the response is encoded, but I wonder what is a good way to extract the data and use it. 

 The request I make to the endpoint is as follows: 

HeaderStr = "Authorization: Bearer " || AccessToken;
Request = New HTTP Request(
			    URL( URLDes ),
		            Method( "Get" ),
			    Headers( {HeaderStr} )
			  ) << Send;

The response I receive is : 

 

"PK"

 

 

Best
Uday
0 Kudos
1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Staff (Retired)

Re: API Response Encoded as Gzip

Not sure how to get a blob back from <<send, but <<download puts the binary data in a file:

request = New HTTP Request(
    URL(
        "http://drawoutside.com/abc123.xlsx"
    ),
    Method( "Get" )
);
request<<download("$temp/z.xlsx");
open("$temp/z.xlsx");

Capture.PNGJMP's open function imported the xlsx file

@bryan_boone

Craige
15 REPLIES 15
Craige_Hales
Staff (Retired)

Re: API Response Encoded as Gzip

"PK" is the signature for .zip files. If the data was received in a blob (text won't have the binary data) you can use JMP's zip archive object to open it, something like this (not at work right now...)

za = open(blob,"zip");

za<<members

za<<open(member)

(best look in the scripting index.)

Craige
Craige_Hales
Staff (Retired)

Re: API Response Encoded as Gzip

Actually, excel files probably are zip files, with an xlsx extension. You might already have exactly what you expect.

Craige
uday_guntupalli
Community Trekker

Re: API Response Encoded as Gzip

@Craige_Hales,
        Appreciate you trying to guide me when not at work. Let's try tomorrow when you are back. But, here is what I did: 

 

image.pngIf the response was indeed a zip file, then this didn't work. I think it is an excel file, because I was able to use WriteBin command and content command in R to write it out as an Excel file. However, I have no clue how to capture it in JMP. 

Additonally, I also tried this : 

 

image.pngThis also results in an error : 

Error with file: Path is invalid: \PK.

 

Best
Uday
0 Kudos
Craige_Hales
Staff (Retired)

Re: API Response Encoded as Gzip

Not sure how to get a blob back from <<send, but <<download puts the binary data in a file:

request = New HTTP Request(
    URL(
        "http://drawoutside.com/abc123.xlsx"
    ),
    Method( "Get" )
);
request<<download("$temp/z.xlsx");
open("$temp/z.xlsx");

Capture.PNGJMP's open function imported the xlsx file

@bryan_boone

Craige
uday_guntupalli
Community Trekker

Re: API Response Encoded as Gzip

@Craige_Hales
        Thank you Craige. Gets me 95% my way there. I see that the file gets downloaded, but JMP fails to open it  both manually or via script. I tried to intoduce a wait() between the download and open and that didn't help either. 

 

image.png

Best
Uday
0 Kudos
Jeff_Perkinson
Community Manager Community Manager

Re: API Response Encoded as Gzip

 


@uday_guntupalli wrote:

I see that the file gets downloaded, but JMP fails to open it  both manually or via script. I tried to intoduce a wait() between the download and open and that didn't help either. 

 

image.png


What's the rest of the error message, above the "In the following script..." line?

 

Are you able to open the downloaded .xlsx file using Excel?

-Jeff
0 Kudos
uday_guntupalli
Community Trekker

Re: API Response Encoded as Gzip

@Jeff_Perkinson,
         Here is the error I receive: 

 

Tried to open: /C:/Users/Test.xlsx

Cannot open table in access or evaluation of 'Open' , Open/*###*/("C:\Users\Test.xlsx")

Best
Uday
0 Kudos

Re: API Response Encoded as Gzip

Additionally,

 

What does

 

 

request << Get Status Message

show?

 

 

That will be the status message from the Web Server (200 is good)

There are:

 

request << Get Status  // a number

//booleans

request << Is Successful

request << Has Error

request << Has Client Error

request << Has Server Error

request << Has Information

request << Has Redirection

 

 

These will let you give information about the connection and the request that was made to the server.

 

 

0 Kudos

Re: API Response Encoded as Gzip

request << Send() will return either a blob or text.

To see what you have call:

request << Get Mime Type();

most of them are covered here:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_ty...

It is up to the server if they actually add mime type information, but these days, most of them do.