0

I am doing an API automation in Ruby using the Faraday Gem to automate a file upload in my API.

I have the follow problem: I need to upload the file as form-data, but it is not working.

Here's the latest syntax that I'm using:

conn = Faraday.new($api['upload']) do |f|
  f.request :multipart
  f.adapter Faraday.default_adapter
end 

formdata = { :file => Faraday::UploadIO.new('./arquivo/pequeno.pdf', 'file/pdf') }
headers = {'Content-Type' => 'multipart/form-data', 'Authorization' => 'Bearer ' + @token, 'uuidUser' => @uuid}

conn.post('/upload', formdata, headers)

Also, I cannot get the response body or code. I got the error that method body or code or status does not exist.

Do you have any idea what I may be doing wrong?

I put the the logger to see further what is happening, and this is the result:

W, [2019-11-27T11:15:15.385754 #4208]  WARN -- : HTTP 500
D, [2019-11-27T11:15:15.386412 #4208] DEBUG -- : "x-content-type-options: nosniff\nx-xss-protection: 1; mode=block\ncache-control: no-cache, no-store, max-age=0, must-revalidate\npragma: no-cache\nexpires: 0\nx-frame-options: DENY\ncontent-type: application/json;charset=UTF-8\ntransfer-encoding: chunked\ndate: Wed, 27 Nov 2019 14:15:14 GMT\nconnection: close\n\n{\"exception\":\"org.springframework.web.multipart.MultipartException\",\"status\":500,\"error\":[\"Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found\"]}"
  • Have you first verified that your Authorization is valid? I.e. can you access other pages with Faraday and the auth headers that you provide? – Casper Nov 25 '19 at 22:05
  • Yes, the Authorization is valid. But the post method is not working. Cant send it. My request is not even generating log in the server. I tried to print the URL sent and it is showing this: # – Vanessa Bioni Nov 26 '19 at 17:38
  • I just tested your code on my server and it posts fine. Have you checked the content of `$api['upload']` to verify that it really does contain something. I.e. the correct URL as text. – Casper Nov 26 '19 at 18:17
  • Yes, I replaced the variable for the real URL. As I said in the previous comment, when I print the "conn" I got this on my console:# – Vanessa Bioni Nov 26 '19 at 20:45
  • Unfortunately there's too little information to be able to solve your problem from here. Try one of the other approaches from here. There are many good answers, including ones that do not require external libraries. https://stackoverflow.com/questions/184178/ruby-how-to-post-a-file-via-http-as-multipart-form-data – Casper Nov 26 '19 at 21:14
  • You could also try to attach a debug logger to Faraday, to see what it is doing internally: https://github.com/envylabs/faraday-detailed_logger If you get some output or new information, you can then add it to your question. – Casper Nov 26 '19 at 21:17
  • I did it, now there is another problem. I updated de question. – Vanessa Bioni Nov 27 '19 at 14:11
  • 1
    I solved the problem. – Vanessa Bioni Nov 27 '19 at 19:51

1 Answers1

1

I solved the problem.

It was on the type of file. Instead of using 'file/pdf' I changed to 'application/pdf' and worked.

For each type of file there is a different way of usage:

pdf:
application/pdf
docx:
application/vnd.openxmlformats-officedocument.wordprocessingml.document