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 = true
request = 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
=> "x1Fx8Bbx00x00x00x00x00x04x00}x93xCBnx830x10Ex7Fx05xB1/x06xFAbx8Dx1CGx15mxDAJ]xF4x11u?xC5x93xC4xAAxB1#lxA2xE4xEFkxC8xA3@b;kxEE=3wxC0xA6xD3m.xBDrx16Fh5xF1xA3 xF4=Tx99xE6B-'~ix17Wx89?e4xD5*x13Rx80u.xCFx11xCAx8CxB7x86OxFCx95xB5xEB1!&[ax0E&0V+f2x9Dx13x9FxD1x17x04x8Ex05xA3xCFxA8xB0xA8xC9GxB08x179xB28x8CoxC2xFB(x8Cx92(xBAx8D)xE9xB1xD0xAFxAAWxAA9xB2$xBEx8BFxE1xE8xDAx19xFFx8BxF4rvxBAxB4xDFxFBxE4,x0Ex9CxDA.xD1x99x90xF8xCAYHxC9xE1D?qx81x85[x0FxAB1x87x14aBIxBBNxC91xFAL(px8Bx83x9Cx17xA0fdUBxE3x91x86xF0xB4AexDBxA5xA6xF7!xCBtxD95xD4LSyx87]~lxE3hx17xD6xCDNamxCBx02yxB3xDDGtxCAnxBBxABvx1AxD4x1DxEDxB6x91CxF4x80xEEx02txFExAAx8Cx05)xEB`MxF2xA2FxD3x15x14KxE4xFBxD6x12xCEgx0ExE9axFAaxB2xDFx1ExA8]nzxDDxAF+x15xBF@txC4SxC2xA1QxBDjxFDEx06xA7x0Eej~x7FWxBAPxA7zxCCxnxEEx17(9xDDx18xD2|xA5xECx0FxA7x9FAx1FxDAx03x00x00"
It should be a zip file, but I dont know how to read this.
I changed the code to…
if response['Content-Encoding'] == 'gzip'
sio = StringIO.new(response.body)
gz = Zlib::GzipReader.new(sio)
decompressed_body = gz.read
else
decompressed_body = response.body
end
Seens to work fine. But i look for the file of many days, and always seens that doesnt have sales. But it has indeed
5
It’s not zip, it’s gzip. Two different things. Use the require 'zlib'
library to decode it.
The example data decodes to:
<?xml version="1.0" encoding="utf-8"?><Conciliation xmlns:xsd="http://schemas.stone.com/"><Header><GenerationDateTime>20240910181152</GenerationDateTime><StoneCode>826170732</StoneCode><LayoutVersion>2.2</LayoutVersion><FileId>0</FileId><ReferenceDate>20240908</ReferenceDate></Header><FinancialTransactions /><FinancialEvents /><FinancialTransactionsAccounts /><FinancialEventAccounts /><Payments /><Trailer><CapturedTransactionsQuantity>0</CapturedTransactionsQuantity><CanceledTransactionsQuantity>0</CanceledTransactionsQuantity><PaidInstallmentsQuantity>0</PaidInstallmentsQuantity><ChargedCancellationsQuantity>0</ChargedCancellationsQuantity><ChargebacksQuantity>0</ChargebacksQuantity><ChargebacksRefundQuantity>0</ChargebacksRefundQuantity><ChargedChargebacksQuantity>0</ChargedChargebacksQuantity><PaidChargebacksRefundQuantity>0</PaidChargebacksRefundQuantity><PaidEventsQuantity>0</PaidEventsQuantity><ChargedEventsQuantity>0</ChargedEventsQuantity></Trailer></Conciliation>
4
The server is not sending a zip file. It’s just standard HTTP compression with GZip and can be handled completely transparently by your HTTP client of choice.
Net::HTTP automatically adds Accept-Encoding for compression of response bodies and automatically decompresses gzip and deflate responses unless a Range header was sent.
Compression can be disabled through the Accept-Encoding: identity header.
https://ruby-doc.org/stdlib-3.1.1/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-Compression
Of course of you don’t want to deal with it send a different Accept-Encoding
header to tell the server that you don’t want the response compressed.
1