Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4623

How to read gzip file download from an API

$
0
0

I`m trying to download a file from an API that give me sales of a client. But I dont know how to do it.

url = URI("https://conciliation.stone.com.br/v1/merchant/{MYCODE}/conciliation-file/20240909")http = Net::HTTP.new(url.host, url.port)http.use_ssl = truerequest = Net::HTTP::Get.new(url)request["accept"] = 'application/xml'request["Authorization"] = 'Bearer USERID'request["X-Authorization-Raw-Data"] = 'ENCRIPTED STRING'request["X-Authorization-Encrypted-Data"] = 'TOKEN'request["Accept-Encoding"] = 'gzip'response = http.request(request)puts response.read_body

Ant that is whats it returns...

[3] pry(Stone)> response.read_body=> "\x1F\x8B\b\x00\x00\x00\x00\x00\x04\x00}\x93\xCBn\x830\x10E\x7F\x05\xB1/\x06\xFA\b\x8D\x1CG\x15m\xDAJ]\xF4\x11u?\xC5\x93\xC4\xAA\xB1#l\xA2\xE4\xEFk\xC8\xA3@\b;k\xEE=3w\xC0\xA6\xD3m.\xBD\r\x16Fh5\xF1\xA3 \xF4=T\x99\xE6B-'~i\x17W\x89?e4\xD5*\x13R\x80u.\xCF\x11\xCA\x8C\xB7\x86O\xFC\x95\xB5\xEB1!&[a\x0E&0V+\f2\x9D\x13\x9F\xD1\x17\x04\x8E\x05\xA3\xCF\xA8\xB0\xA8\xC9G\xB08\x179\xB28\x8Co\xC2\xFB(\x8C\x92(\xBA\x8D)\xE9\xB1\xD0\xAF\xAAW\xAA9\xB2$\xBE\x8BF\xE1\xE8\xDA\x19\xFF\x8B\xF4\rv\xBA\xB4\xDF\xFB\xE4,\x0E\x9C\xDA.\xD1\x99\x90\xF8\xCAYH\xC9\xE1D?q\x81\x85[\x0F\xAB1\x87\x14aBI\xBBN\xC91\xFAL(p\x8B\x83\x9C\x17\xA0\fdUB\xE3\x91\x86\xF0\xB4Ae\xDB\xA5\xA6\xF7!\xCBt\xD95\xD4LSy\x87]~l\xE3h\x17\xD6\xCDNam\xCB\x02y\xB3\xDDG\t\xCA\n\xBB\xABv\x1A\xD4\x1D\xED\xB6\x91C\xF4\x80\xEE\x02\t\xFE\xAA\x8C\x05)\xEB`M\xF2\xA2F\xD3\x15\x14K\xE4\xFB\xD6\x12\xCEg\x0E\xE9\a\xFA\a\xB2\xDF\x1E\xA8]nz\xDD\xAF+\x15\xBF@t\xC4S\xC2\xA1Q\xBDj\xFDE\x06\xA7\x0E\ej~\x7FW\xBAP\xA7z\xCCxn\xEE\x17(9\xDD\x18\xD2|\xA5\xEC\x0F\xA7\x9FA\x1F\xDA\x03\x00\x00"

It should be a zip file, but I dont know how to read this.


I changed the code to...

tf = Tempfile.new(["temp", ".zip"])response = http.request(request)tf.write(response.read_body.force_encoding("UTF-8"))

Now it seens to be good. it returns an integer (361 in the case).

And tf looks like

#<File:/var/folders/7q/ytrrfzr532n87tlywvwgvspm0000gn/T/temp20240910-61208-ojgij9.zip>

My question now is: How I read the file?


Viewing all articles
Browse latest Browse all 4623


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>