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:
My two problems are:
data[]
ones work fine.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.
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
.