Uploading multiple files in the same request

Andres

I need to create a POST where I can upload multiple files in the same request, but I don't know how to write this with grape. Right now to upload just one file this is what I'm doing and It's working fine:

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 it's not looking fine:

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 good result either.

enter image description here

What's the right way of handling this?

Andres

After some testing I found the answer, I'll 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 can't test this using swagger (or at least I didn't found a way of doing it), 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

FineUploader uploading multiple files as one request

From Dev

Uploading multiple files with one request in Podio (suggestion)

From Dev

Issue uploading multiple files of the same name

From Dev

Mysql updating multiple textboxes and uploading multiple files at the same time

From Dev

Mysql updating multiple textboxes and uploading multiple files at the same time

From Dev

Uploading files and JSON data in the same request with jquery ajax?

From Dev

Uploading files and JSON data in the same request with jquery ajax?

From Dev

Uploading multiple files in a ModelForm

From Dev

AngularJs Uploading multiple files .then

From Dev

Multiple Files Uploading in Laravel

From Dev

Issues with Uploading Multiple files with PHP

From Dev

Multiple files uploading in multidimensional array

From Dev

PHP code for uploading multiple files

From Dev

Multiple files uploading not working in Codeigniter

From Dev

Uploading multiple files using Javascript

From Dev

Uploading multiple files and renaming - PHP

From Dev

Multiple files not uploading in play framework

From Dev

Multiple files uploading not working in Codeigniter

From Dev

Uploading multiple files in Laravel 5

From Dev

FileSystem uploading files vs HTTP Request in Laravel?

From Dev

Uploading files to the same directory as the script using php

From Dev

Google script keep uploading files in Same folder

From Dev

Media uploader "uploading" multiple instances of the same image

From Dev

Media uploader "uploading" multiple instances of the same image

From Dev

PHP: Uploading multiple images at the same time

From Dev

Uploading multiple files in single multipart post

From Dev

Uploading then renaming multiple files over ssh

From Dev

Selecting multiple files and uploading them using Jersey

From Dev

Uploading multiple files with multer, but from different fields?

Related Related

  1. 1

    FineUploader uploading multiple files as one request

  2. 2

    Uploading multiple files with one request in Podio (suggestion)

  3. 3

    Issue uploading multiple files of the same name

  4. 4

    Mysql updating multiple textboxes and uploading multiple files at the same time

  5. 5

    Mysql updating multiple textboxes and uploading multiple files at the same time

  6. 6

    Uploading files and JSON data in the same request with jquery ajax?

  7. 7

    Uploading files and JSON data in the same request with jquery ajax?

  8. 8

    Uploading multiple files in a ModelForm

  9. 9

    AngularJs Uploading multiple files .then

  10. 10

    Multiple Files Uploading in Laravel

  11. 11

    Issues with Uploading Multiple files with PHP

  12. 12

    Multiple files uploading in multidimensional array

  13. 13

    PHP code for uploading multiple files

  14. 14

    Multiple files uploading not working in Codeigniter

  15. 15

    Uploading multiple files using Javascript

  16. 16

    Uploading multiple files and renaming - PHP

  17. 17

    Multiple files not uploading in play framework

  18. 18

    Multiple files uploading not working in Codeigniter

  19. 19

    Uploading multiple files in Laravel 5

  20. 20

    FileSystem uploading files vs HTTP Request in Laravel?

  21. 21

    Uploading files to the same directory as the script using php

  22. 22

    Google script keep uploading files in Same folder

  23. 23

    Media uploader "uploading" multiple instances of the same image

  24. 24

    Media uploader "uploading" multiple instances of the same image

  25. 25

    PHP: Uploading multiple images at the same time

  26. 26

    Uploading multiple files in single multipart post

  27. 27

    Uploading then renaming multiple files over ssh

  28. 28

    Selecting multiple files and uploading them using Jersey

  29. 29

    Uploading multiple files with multer, but from different fields?

HotTag

Archive