Serving a file from your Rails app from another server protected by basic authentication

Interesting problem: Retrieve a document from one web service protected by basic auth (or some authentication mechanism) and serve it via a public link on another website.

It ended up boiling down to this code in the controller (reduced to be in the same method instead of factored out as it was.)

  def get_download
    response = Net::HTTP.start('localhost', '3010', {} ) do |http|
      request ='http://localhost:3010/my_document.pdf')
      request.basic_auth 'username', 's3cretpassword'
      http.request(request) do |response|
        send_data(response.body, filename: 'set_filename.pdf')

%d bloggers like this: