How can I send binary data from Sinatra?

Nerian picture Nerian · May 21, 2011 · Viewed 8.6k times · Source

I want to send binary data from a Sinatra application so that the user can download it as a file.

I tried using send_databut it gives me an undefined method 'send_data'

How could I achieve this?

I could write the data to a file and then use send_filebut I would rather avoid doing this.

Answer

Konstantin Haase picture Konstantin Haase · May 24, 2011

you can just return binary data:

get '/binary' do
  content_type 'application/octet-stream'
  "\x01\x02\x03"
end