Extract files from zipped, raw API's response

Viewed 27

I am working with this API which allows me to retrieve balance-sheets from selected firms. When sending the request, it send back a application/zip content as can be seen in the headers or on the API's swagger, at 'Document'.

Then, I am a little lost on how I can access the files.

Using a standard rawToChar() returns the following error:

Error in rawToChar(bilan_X_firm_X$content) :        #bilan_X_firm_X being the variable where I saved the full API's response
  embedded nul in string: 

I read that it might be a common C implementation in R that is problematic. But I am quite noob in working with APIs and with raw files. So there might be another proper solution to open an archive which is coded in raw (UTF-8 here) format.

Do you have any idea how to get the desired files from this raw content please?

My ultimate goal is to get balance-sheet's informations on many firms in an automated way, so the solution have to be as light as possible.

Please find here a reproducible exemple. NB: you must have an account to access the APIs (please send me a pm so I share my cred. Not sure if I can make them public).

Thank you for your time.

if(!require(tidyverse)) install.packages("tidyverse")
if(!require(httr)) install.packages("httr")
if(!require(jsonlite)) install.packages("jsonlite")

### Credentials
login <- 'xxxxxxxxx'
login_pw <- 'yyyyyyyy'

### Connect to INPI open data
log <- httr::POST('https://opendata-rncs.inpi.fr/services/diffusion/login',
            add_headers(login = login, password = login_pw))

### Get the auth cookie
cookie <- cookies(log)


## Request API for firm_X available documents
firm_X <- httr::GET('https://opendata-rncs.inpi.fr/services/diffusion/bilans/find?siren=423108356',
 
                set_cookies = cookie)

# Convert the content so it's readable
content_firm_X <- jsonlite::fromJSON(rawToChar(firm_X$content))

# Get the id (or ids) of files that you want to obtain
id_bilan_X_firm_X <- content_firm_X$idFichier[length(content_firm_X$idFichier)]     

# Eventually, get the desired response 
bilan_X_firm_X <- httr::GET(url = str_c("https://opendata-rncs.inpi.fr/services/diffusion/document/get?listeIdFichier=", toString(id_bilan_X_firm_X)), 
                                 set_cookies = cookie)
0 Answers
Related