Question

I am trying to download a file from a URL and pass it to my user.

In order for this to work I need to construct a HTTP GET request like this:

https://some.service.com/api/pdf?doc_id=99&data[info1]=some+info&data[info2]=more+info

This works fine when I issue it in my browser and I get the correct document including the correct data returned.

I want to do the same thing programmatically:

  • Build the query and request the file via rest-client.
  • Serve the file to the user via send_file.

My two problems are:

  1. I am making a mistake in the way I pass my params into rest-client, but I have found no means of checking what the resulting query actually is. This makes it hard for me to see what I am doing wrong, since I would just compare the resulting call with my works-in-my-browser call. All parameters, except for the data[] ones work fine.
  2. I can only serve the correct document (minus the correct data) via send_data, which means I have to specify type and filename again. This doesn't make sense since the PDF I am downloading is already all good to go with naming and everything. send_file returns an error.

Here's my code:

def request_and_send_file
  response = RestClient.get("https://some.service.com/api/pdf", :params => {
    doc_id: 99,
    :data => {
      info1: "some info",
      info2: "more info"}})
  send_data response.body, filename: "hochzeitsfeder.pdf", disposition: "attachment", :content_type => 'application/pdf'
end

Any help is greatly appreciated.

Was it helpful?

Solution

I was able to solve the problem with the parameters. The parameters work if I make the call like this:

def request_and_send_file
  response = RestClient.get("https://some.service.com/api/pdf", :params => {
    doc_id: 99,
    "data[info1]" => "some info",
    "data[info2]" => "more info"})
  send_data response.body, filename: "customer.pdf", disposition: "attachment", :content_type => 'application/pdf'
end

What I still could not figure out, is why I cannot serve the file I get from the HTTP call via send_file, but have to use send_data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top