title: Forward a file download in sinatra using streaming
body: <p>I have made a ruby / sinatra website and I need to let the user to download a file.
   This file is not local hosted, it is hosted on a remote API. end user must not see the true origin of the file.</p>
  
  <pre><code>get "/files/:elementKey/masterfile" do
    content_type "application/octet-stream"  
    loadMasterfile(params[:elementKey])
  end
  </code></pre>
  
  <p>With loadMasterfile: </p>
  
  <pre><code>http = Net::HTTP.new(plainURI,443)
   http.use_ssl = true; 
   http.start do |http|
     req = Net::HTTP::Get.new(resource, {"User-Agent" =&gt;"API downloader"})
     req.basic_auth(user.keytechUserName, user.keytechPassword)
     response = http.request(req)     
     # return this as a file attachment
     attachment( response["X-Filename"])  #Use the sinatra helper to set this as filename
     response.body  &lt;&lt; This lets sinatra download the file and then forward the whole content to the browser  
   end
  </code></pre>
  
  <p>This code works, but: 
   The file is downloaded first to the ruby/sinatra and then forwarded to the browser. 
   User must wait until download starts - browser seems to freeze. </p>
  
  <p>Is there a solution to start a download form a remote API and forward the contents in one flow? </p>
  
  <p>I found nothing about that or just found solutions for local file downloads, but I must download a file from a remote API. </p>
  
  <p>I also can not cache the file locally or on Amazon AWS. </p>
  
  <p>Any Ideas? </p>