Morgan
Morgan

Reputation: 1449

Ruby Post Request: PDF File

The API example for uploading documents is this

headers = {
    'Authorization': 'Bearer ACCESS_TOKEN',
}
params = {
    'doctor': 'https://drchrono.com/api/doctors/1234',
    'patient': 'https://drchrono.com/api/patients/5678',
    'description': 'Short document description here',
    'date': '2014-02-24',
    'metatags': json.dumps(['tag1', 'tag2']),
}
with open('/path/to/your.pdf', 'rb') as f:
    files = {'document': f}
    requests.post(
        'https://drchrono.com/api/documents',
        data=params, files=files, headers=headers,
    )

I use Prawn to create a PDF. One route automatically downloads the PDF while the other renders it to be viewed in the browser. I was running into issues so (to try to figure out whether it was a Prawn PDF issue or a PDF issue) I downloaded a PDF off the net that was rather basic. Same issues. I'm using HTTParty to send my POST request.

  headers = {'Authorization' => 'Bearer ' + access_token}
  File.open("#{Rails.root}/app/assets/test.pdf", "rb") do |file|
    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 = HTTParty.post('https://drchrono.com/api/documents', :headers => headers, :body => params)
    puts response
    data = JSON.parse(response.body)
    puts data
 end

I receive the following error.

{"document"=>["No file was submitted. Check the encoding type on the form."]}

I initially thought perhaps "document" wasn't supposed to be included in the body directly under the key document, but when I commented out my params "document" then I received this error.

{"document"=>["This field is required."]}

So it seems it is reading the document key and expecting to get a document value but isn't. If I change file.read to just file I receive the same error

{"document"=>["No file was submitted. Check the encoding type on the form."]}

I feel like the answer is probably extra-simple, but I've been stuck for a while now. Any ideas?

Upvotes: 3

Views: 2584

Answers (1)

Patrick Oscity
Patrick Oscity

Reputation: 54684

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

Upvotes: 5

Related Questions