How to use an HTTP response containing a pdf document as a Paperclip attachment?

I am sending an HTTP GET request to a third-party API that responds with binary content (a pdf document). I would like to use the response body as a paperclip attachment.

response = download_document(id)
consultation = Consultation.new
consultation.document = File.open("testing.pdf", "wb+") {|f| f.write(response) }
consultation.save

Using the Faraday gem to make the HTTP GET request, I can see the following output from the server:

pragma: "no-cache"
content-length: "16080"
content-type: "application/octet-stream"
expires: "-1"
server: "Microsoft-IIS/8.5"
content-disposition: "attachment; filename=2160506180802980077.pdf"
x-aspnet-version: "4.0.30319"
set-cookie: "XDEBUG=; path=/"
p3p: "CP=\"NOI OUR PSA DEV PSD STA COM CUR\""
date: "Sun, 08 May 2016 03:38:41 GMT"
connection: "close"

The code fails with the following error:

Paperclip::AdapterRegistry::NoHandlerError: No handler found for 16080

It looks like somehow Paperclip is reading the content-length as the content-type.

I’m looking for some help either to resolve the issue above, or propose an alternative method to read an octect-stream from an HTTP GET response in as a Paperclip attachment.

What kind of object does download_document return? (What’s its class?).

download_document returns a String. When I inspect the returned object, it appears to be a binary representation of the PDF document (see below). Do I have to encode this somehow before writing it to a file?

"%PDF-1.5\r\n%\xE2\xE3\xCF\xD3\r\n1 0 obj\r\n<</ModDate(D:20160508071037-07'00')/CreationDate(D: 20160508071037-07'00')/Creator(Axacore)/Producer(Axacore PDF library)>>\r\nendobj\r\n3 0 obj\r\n<</Type/XObject/Subtype/Image/Width 1728/Height 2162/BitsPerComponent 1/ColorSpace/DeviceGray/Length 14960/Filter[/CCITTFaxDecode]/DecodeParms [<< /Columns 1728 /Rows 2162 /K -1>>]>>\r\nstream\r\n\xFC\xEC\xCD\x10\xA4w\xAA\xFF;5g\xD9\x9A'2\xE6\x91\xF2'$\x8F\x919\x12\x11\f\x926!\xF1J\f\x81#\xAA)\x04>!obj\\r\\n<</Length 99>>\\r\\nstream\\r\\nq\\r\\n1 0 0 1 0 0.000000000000000 cm\\r\\n609.882352941176000 0 0  obj\\r\\n<</Type/Page/Parent 2 0 R/Rotate 0/MediaBox[0 0 609.882352941176 794.204081632653]/Resources 5 0 R/Contents 4 0R>>\\r\\nendobj\\r\\n2 0 obj\\r\\n<</Type/Pages/Count 1/Kids[ 6 0 R]>>\\r\\nendobj\\r\\n7 0 obj\\r\\n<</Type/Catalog/Pages 2 0 R/PageMode/UseThumbs>>\\r\\nendobj\\r\\nxref\\r\\n0 8\\r\\n0000000000 65535 f\\r\\n0000000017 00000 n\\r\\n0000015713 00000 n\\r\\n0000000159 00000 n\\r\\n0000015341 00000 n\\r\\n0000015492 00000 n\\r\\n0000015579 00000 n\\r\\n0000015768 00000 n\\r\\ntrailer\\r\\n<</Size 8/Root 7 0 R/Info 1 0 R>>\\r\\nstartxref\\r\\n0000015835\\r\\n%%EOF\\r\\n\""