I'm trying to read a response from a marketplace web service. Every other response from this web service is returned in XML format. However, this particular call is requesting a file download. I'm unfamiliar with the way in which it's returned. After looking at the contents, there is XML as well as encoded binary data which is in there as some sort of attachment.
The request I make looks like this. The request is a simple XML request:
begin
response = Net::HTTP.start(url.host, url.port, :use_ssl => url.scheme == 'https') do |http|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.request(request)
end
rescue Errno::ECONNRESET => e
count += 1
retry unless count > 10
puts "Tried 10 times and couldn't get #{url.host}: #{e}"
end
Here is what the response.body
looks like:
--MIMEBoundaryurn_uuid_AF2837F4196B2631EC15070889135182607126
Content-Type: application/xop+xml; charset=utf-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <0.urn:uuid:AF2837F4196B2631EC15070889135182607127>
<?xml version='1.0' encoding='UTF-8'?>
<downloadFileResponse xmlns="http://www.marketplace.com/marketplace/services">
<ack>Success</ack>
<version>1.1.0</version>
<timestamp>2017-10-04T03:48:33.518Z</timestamp>
<fileAttachment>
<Size>25895</Size>
<Data><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:urn:uuid:E3A8215C82DBC51E6D1507090865513"/></Data>
</fileAttachment>
</downloadFileResponse>
--MIMEBoundaryurn_uuid_AF2837F4196B2631EC15070889135182607126
Content-Type: application/zip
Content-Transfer-Encoding: binary
Content-ID: <urn:uuid:E3A8215C82DBC51E6D1507090865513>
PK&úCKÃEK¬gdi∂6509805153_report.xmlUT hH‘YhH‘Yux00ÏùÎs‚8∂¿ø˜_°€üÓ≠=-â Ù6ÚÍ< ›”≥µïr@ NåÕ⁄&è˘ÎØÑÅêàçÕ‡…¶+ùnåd=|Œ—œÁ˱ۜáæÓT:æ˜Îg¥?Âu¸Æ„]ˇ˙y]ïƒÁ~˘¥≥;tokvd◊:=€ªVM|/T!–˘ΩP'
º≤∫¥Àˆ¿ Àj˜x◊U’ÔÎT 㬜˙-flÌ6’¿¢/ü>Ìú]‘Td;n¯e¸Ò∞ˆ L%‰åqÀ*!é, òdB±≥=Ic™Û®ÇÛpÙ©ÁªÆ≥ŸA∏≥=˚≈8ŸûÑ—©›W_v˛Á_’Z•]˘W€$Ó‹˛˚fl_∆9û“å3€/Ûòbº)œ4…8KΩØõÚî>äÀË≈Ÿ˛Ít\ÿ›Í¯˝ß{ƒy∆7hÙt_=›ÄC$·Ä3åürƒâtgˆúASuúÅ£ªwnοlç_'èo—ä•"ÙîAÇ)–ÆÔ{˜ v£ÿuÔ∫”õL2Ãf«œæ√„Ô™NÙ¯ºb{∂\Ÿ”{MSLnfGÍ,h˛ù„ufÚ}ØÃˇ<Mú≥·áëÌV˝ÓL&åuKJÿBhöy&Ÿ∏䲖ãǵ<o=UpÊ˚8«@ÎEKwŒl˝Œ-∞Ë¥O›4õÓ”N√~ÏÎ~Ø∫ T∑ÌË€aàx ¡$mƒ
...
--MIMEBoundaryurn_uuid_AF2837F4196B2631EC15070889135182607126--
Obviously I can see there is zip data here. But I've read every other response with Hash.from_xml
and obviously that's not going to work here.
Update
If I write the string to a file test.zip
, I can unzip it at the Linux command line, and it creates a readable XML file after flashing this warning:
Archive: test.zip
warning [test.zip]: 822 extra bytes at beginning or within zipfile
(attempting to process anyway)
inflating: 6509805153_report.xml
Not sure what the extra bytes it's complaining about are.
Update 2
That's definitely the MIME header and the XML envelope. I can confirm that if I strip those characters out by hand, and the MIME footer, then the test file unzips without warning.
So this appears to be a zipped XML envelope containing a zip file.