0

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
TIM
  • 315
  • 4
  • 14
  • Ruby has debuggers which make it very easy to drop into your code and single-step each line, allowing you to see parameters are they're being built. If the code in a method you're calling is pure Ruby you can also step into it and drill down. I'd recommend looking at PRY and pry-debugger; You can learn a lot, plus KNOW your code is doing what you expect. – the Tin Man Sep 20 '13 at 18:39

1 Answers1

2

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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
TIM
  • 315
  • 4
  • 14
  • 1
    You use `send_data` because the content you want to send isn't a file, it's data in memory. A file is data on the disk. The `path` parameter lets you specify that file. See [the documentation](http://api.rubyonrails.org/classes/ActionController/DataStreaming.html) for more information. – the Tin Man Sep 20 '13 at 18:32