I'm working on a Rails app to send Word files I have stored on Amazon S3 to convertapi for conversion into PDFs. I'm using the paperclip gem to manage the files, and the curb gem to make the actual request.
# model with property has_attached_file :attachment
def convert_docx_to_pdf
base = "https://do.convertapi.com/Word2Pdf"
api_key = '*****'
file = open(attachment.url)
c = Curl::Easy.new(base)
c.multipart_form_post = true
c.http_post(
Curl::PostField.file('thing[file]', file.path),
Curl::PostField.content('ApiKey', api_key)
)
end
I'm attempting to follow the documentation for curb here.
When I run this from the rails console it simply returns true
. I'd like to capture the resulting PDF.
(I've verified that this works if I upload the file manually on convertapi's test endpoint tool.)
UPDATE 09.18.15
I implemented the changes suggested by Jonas. Here's the new code:
def convert_docx_to_pdf
base = "https://do.convertapi.com/Word2Pdf"
api_key = ENV['CONVERTAPI_API_KEY']
file = open(attachment.url)
Curl::Easy.new('https://do.convertapi.com/Word2Pdf') do |curl|
curl.multipart_form_post = true
curl.http_post(Curl::PostField.content('ApiKey', api_key), Curl::PostField.file('File', file.path))
return curl.body_str
end
end
Still no luck, curl.body_str
returns just "Bad Request"
.
(file.path = /var/folders/33/nzmm899s4jg21mzljmf9557c0000gn/T/open-uri20150918-13136-11z00lk
)