-1

actually i'm writing a ruby script which accesses an API based on HTTP-POST calls. The API returns a zip file containing textdocuments when i call it with specific POST-Parameters. At the moment i'm doing that with the Net::HTTP Package.

Now my problem: It seems to return the zip-file as a string as far as i know. I can see "PK" (which i suppose is part of the PK-Header of zip-files) and the text from the documents.

And the Content-Type Header is telling me "application/x-zip-compressed; name="somename.zip"".

When i save the zip file like so:

result = comodo.get_cert("<somenumber>")
puts result['Content-Type']
puts result.inspect
puts result.body

File.open("test.zip", "w") do |file|
    file.write result.body
end

I can unzip it on my macbook without further problems. But when i run the same code on my Win10 PC it tells me that the file is corrupt or not a ZIP-file.

Has it something to do with the encoding? Can i change it, so it's working on both? Or is it a complete wrong approach on how to recieve a zip-file from a POST-request?

PS: My ruby-version on Mac:

ruby 2.2.3p173

My ruby-version on Windows:

ruby 2.2.4p230

Many thanks in advance!

2 Answers2

0

The problem is due to the way Windows handles line endings (\r\n for Windows, whereas OS X and other Unix based operating systems use just \n). When using File.open, using the mode of just w makes the file subject to line ending changes, so any occurrences of byte 0x0A (or \n) are converted into bytes 0x0D 0x0A (or \r\n), which effectively breaks the zip.

When opening the file for write, use the mode wb instead, as this will suppress any line ending changes.

http://ruby-doc.org/core-2.2.0/IO.html#method-c-new-label-IO+Open+Mode

Alexa Y
  • 1,854
  • 1
  • 10
  • 13
0

Many thanks! Just as you posted the solution i found it out myself..

So much trouble because of one missing 'b' :/

Thank you very much!

The solution (see Ben Y's answer):

result = comodo.get_cert("<somenumber>")
puts result['Content-Type']
puts result.inspect
puts result.body

File.open("test.zip", "wb") do |file|
    file.write result.body
end
Community
  • 1
  • 1