Sunday, 15 February 2015

Ruby on rails + api rest + upload pdf -


I have been lost with a post request to upload the file.

I generate a PDF file with shrimp and I save it in my public folder

Then I have tried this file to request a post what I have tried Want to upload:.

  local_contract_folder_path = Rails root.join ( "public / pdfs /") local_contract_file = local_contract_folder_path.join ( "contrat.pdf") post_file = HTTParty.post (contrat_file_url ,: query = & gt; {: filename = & gt; File.new (local_contract_file }},: Detect_mime_type = & gt; true: body => gt; {}:: header => {'content-type' => 'multipart / form-data'} ,: basic_auth = & Gt; Certification)  

The response is:

  {Errors: {"file name" = & gt; [], "" = & Gt; [ "FileMustBePDF"]}, Message: Your request is invalid}  

My API documentation have this instruction:

request body to the binary file itself Be sure to enter the filename in the URL. Also, do not use multipart / form-data encoding.

I tried to use it but it is not working.

Do you have any idea

Edit / Solution:

Here is a PDF file with HTTParty solution to upload :

  local_contract_folder_path = Rails.root.join ( "public / pdfs /") local_contract_file = local_contract_folder_path.join ( "contrat.pdf") contract_file = File.open (local_contract_file, 'rb') . Read post_file = HTTParty.post (contrat_file_url ,: body = & gt; contract_file ,: headers = & gt; { 'content-type' = & gt; 'application / pdf'} ,: basic_auth = & gt; authentication)  


No comments:

Post a Comment