Upload multiple files to the same request

I need to create a POST where I can upload multiple files to the same request, but I don’t know how to write this using grapes. Right now, to upload only one file, this is what I am doing and it works great:

desc 'Creates a new attachment.' params do requires :file, :type => Rack::Multipart::UploadedFile, :desc => "Attachment File." end post do attachment = Attachment.new attachment.file = ActionDispatch::Http::UploadedFile.new(params[:file]) attachment.save! attachment end 

Swagger shows me this:

enter image description here

I was thinking of doing something like this:

 desc 'Creates a new attachment.' params do requires :file, :type => Array[Rack::Multipart::UploadedFile], :desc => "Attachment File." end 

But this does not look good:

enter image description here

Also I tried:

 params do optional :attachments, type: Array do requires :file, :type => Rack::Multipart::UploadedFile, :desc => "Attachment File." end end 

Not a very good result.

enter image description here

What is the right way to handle this?

+5
source share
1 answer

After some testing, I found the answer, I will leave it here:

 params do requires :first_name, type: String, desc: "User first name" requires :last_name, type: String, allow_blank: false, desc: "User last name" optional :attachments, type: Array do requires :file, :type => Rack::Multipart::UploadedFile, :desc => "Profile pictures." end end 

You cannot verify this with swagger (or at least I have not found a way to do this), but you can use this curl:

 curl -v -F "first_name=John" -F "last_name=McTest" -F "attachments[][file] =@first _picture.png" -F "attachments[][file] =@second _picture.jpg" http://url/api/users 
+7
source

Source: https://habr.com/ru/post/1216463/


All Articles