Ruby Post Request: PDF File

Ruby Post Request: PDF File

Maybe you need to send it as a multipart request. Since you are already using HTTParty, you may find the httmultiparty Gem useful. Example:

require 'httmultiparty'

class DrChronoClient
include HTTMultiParty
base_uri 'https://drchrono.com/api'
end

File.open("#{Rails.root}/app/assets/test.pdf", "rb") do |file|
headers = {
:Authorization => 'Bearer ' + access_token
}

params = {
:document => file.read,
:doctor => 'https://drchrono.com/api/doctors/' + doctor.id,
:patient => 'https://drchrono.com/api/patients/' + patient.id,
:description => 'Report',
:date => date
}

response = DrChronoClient.post('documents', :query => params, :headers => headers)
puts response
data = JSON.parse(response.body)
puts data
end

Ruby: How to post a file via HTTP as multipart/form-data?

I like RestClient. It encapsulates net/http with cool features like multipart form data:

require 'rest_client'
RestClient.post('http://localhost:3000/foo',
:name_of_file_param => File.new('/path/to/file'))

It also supports streaming.

gem install rest-client will get you started.

Return pdf file from show action

First, get the file:

require 'open-uri'

download = open('https://some-url-com')

IO.copy_stream(download, 'tmp/my_document.pdf')

Then send the file as part of your JSON:

pdf_filename = File.join(Rails.root, 'tmp/my_document.pdf')

# add this to your JSON where desired
send_file(pdf_filename, :filename => 'your_document.pdf', :type => 'application/pdf')

How get pdf file from httparty get response

You can do it like this:

filename = "tmp/test.pdf"
File.open(filename, "w") do |file|
file.binmode
HTTParty.get(URL_TO_PDF, stream_body: true) do |fragment|
file.write(fragment)
end
end

Replace URL_TO_PDF with your pdf url

How to upload pdf files to server from ajax without rails on ruby 2.4

To access the param values by key, use tic marks. Here's the solution I went with, which includes a method for creating a file directory where it doesn't already exist. To write the file use file.open and wb to write the file in binary mode.

require 'fileutils'

def create_folder(path)
unless File.directory?(path)
FileUtils.mkdir_p(path)
end
end

post 'medcon/adddocs' do |params, me|
params = params['body']
filename = params['data']['filename']
fid = params['fid'].to_s
file = Base64.decode64(params['data']['file_base64'])
folder = "../data/medcon/fid#{fid}"

create_folder folder # send folder path to create folder method

File.open("#{folder}/#{filename}", 'wb') do |f|
f.write(file)
end

end

Net::HTTP get a PDF file and save with paperclip

Assuming the PDF object you're getting is okay (I'm not 100% sure it is), then you could do this:

file = StringIO.new(attachment) #mimic a real upload file
file.class.class_eval { attr_accessor :original_filename, :content_type } #add attr's that paperclip needs
file.original_filename = "your_report.pdf"
file.content_type = "application/pdf"

then save the file with Paperclip.

(from "Save a Prawn PDF as a Paperclip attachment?")

Wrapping an application/pdf file in ruby File

The response you are logging contains both the header and PDF file itself in the body.

I'd just save that into a temp file:

f = Tempfile.open("/tmp/response.pdf") do |fh|
response.body # or the method to get the body from your response object
end

and then upload your file f to your favorite cloud service.



Related Topics



Leave a reply



Submit