Sending file with Goliath/Grape

378 views
Skip to first unread message

Michael Martin Moro

unread,
May 16, 2013, 1:24:05 PM5/16/13
to ruby-...@googlegroups.com
Good day !

I'm working on a tool that allow people to download audio files.
Unfortunately I've been unable to find any information about rendering binary files through Grape. Is it possible ? Or is it rather something that should be done only by interacting with Goliath ? Or is using Goliath/Grape to serve files a really bad idea ?

Thanks for reading.

Daniel Doubrovkine

unread,
May 16, 2013, 1:27:22 PM5/16/13
to ruby-...@googlegroups.com
You should just be able to set the proper content-type and return binary data. We certainly do that for images (in this example as attachments).

header "Content-Disposition", "attachment; filename*=UTF-8''#{URI.escape(filename)}"
header "Content-Type", "image/jpeg"
data


--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--

dB. | Moscow - Geneva - Seattle - New York
dblock.org - @dblockdotorg

Michael Martin Moro

unread,
May 17, 2013, 4:16:14 AM5/17/13
to ruby-...@googlegroups.com
Thanks for the tip !

It does work for text files and such, the mimetype is sent properly. However for some reason it tries to read the data that I send back, and fails whenever a non-ascii character is met, which limits the possible contents for a text file, and prevents from sending any binary files. I end up with an error that looks like this:

{"error":"\"\\x89\" on US-ASCII"}

Is there something missing to get binary support ?

Daniel Doubrovkine

unread,
May 17, 2013, 7:44:27 AM5/17/13
to ruby-...@googlegroups.com
Two things:

1) Make sure you have the correct content-type, that's not becoming JSON or something like that. For example content_type :pdf, "application/pdf".
2) Make sure the encoding of your data is binary. Check what data.encoding is. If you're serving a file, make sure to open it in "rb" binary mode.

If everything fails, post your code. 

Michael Martin Moro

unread,
May 22, 2013, 12:58:48 PM5/22/13
to ruby-...@googlegroups.com
Indeed there was a format json somewhere that shouldn't have been here.
It works fine now, thanks for the help !
Reply all
Reply to author
Forward
0 new messages