Uploading binary file via grape API

2,655 views
Skip to first unread message

Deepak

unread,
Jun 3, 2013, 1:55:47 PM6/3/13
to ruby-...@googlegroups.com
I want to upload a binary file via grape. My API is json, but I want to have one of the params be a binary file. 
The binary data format is application/x-pkcs12. This is what I am trying to do.

params do
  requires :param1, type: Binary # What should I specify here for type?
  requires :param2, type:String
post '/upload_file' 
   process_input(params[:param1], params[:param2]) # Need both the binary and the string for processing.
end

Is the above code supported in grape. How do I structure the curl command to input binary data as part of a json hash?

Thank you,
Deepak.

Daniel Doubrovkine

unread,
Jun 3, 2013, 2:21:42 PM6/3/13
to ruby-...@googlegroups.com
What would a post for something like this look like? A multipart thing?


--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--

dB. | Moscow - Geneva - Seattle - New York
code.dblock.org - @dblockdotorg - artsy.net - github/dblock

Deepak

unread,
Jun 18, 2013, 2:08:20 PM6/18/13
to ruby-...@googlegroups.com
Daniel,
 I am not sure I follow the question. I am trying to provide an API that allows the client to upload a binary blob + a string param. Since posting this question,  I figured, I can even try moving the string param to the header (curl -H 'key: value' ) and just upload the entire binary blob in the request body. I am still having issues uploading the binary.
I tried the solution here - https://groups.google.com/forum/#!searchin/ruby-grape/binary/ruby-grape/2mvMnMf8zYY/axAG2LnMmfcJ but this solution is not working for me - I get this error instead ->  ("error":"The requested format 'json' is not supported. )

Will appreciate any help or pointers you can provide. Will also appreciate if you or others in the community can add documentation regarding uploading/downloading binary data via Grape. 

Thank you

Daniel Doubrovkine

unread,
Jun 18, 2013, 8:03:54 PM6/18/13
to ruby-...@googlegroups.com
Got it. It's pretty straightforward. There's a demo in https://github.com/dblock/grape-on-rack for uploading an image, and it's the same for any binary file.

  class API < Grape::API
    format :json
    desc "Upload a binary file."
    params do
      requires :name, type: String
      requires :data # omit type
    end
    post :binary do
      {
        name: params[:name],
        filename: params[:data][:filename],
        size: params[:data][:tempfile].size
      }
    end
  end

This lets you combine JSON data, form data or data from a query string and an actual piece of data from a file like so:

curl -X POST -i -F name=test -F data=@spec/fixtures/grape_logo.png http://localhost:9292/binary

Daniel Doubrovkine

unread,
Jun 18, 2013, 8:05:01 PM6/18/13
to ruby-...@googlegroups.com
Downloading sample in https://github.com/dblock/grape-on-rack/blob/master/api/content_type.rb, you just need to set the proper content-type for the data. Probably "application/binary" is good enough for "random binary data", but you need to declare this in Grape, so

content_type :binary, "application/binary" 

Daniel Doubrovkine

unread,
Jun 18, 2013, 8:07:36 PM6/18/13
to ruby-...@googlegroups.com
Sorry, "application/octet-stream" is the right content type for random binary content.

Deepak

unread,
Jun 19, 2013, 12:47:08 AM6/19/13
to ruby-...@googlegroups.com
Thanks for the response. I was using pkcs12, so I did 

content_type :p12, "application/x-pkcs12" 

Also, I changed my GET/download API to GET '/download/keys.p12' instead of GET '/download/keys' and now it is working. (I was having issues with downloading binary as well).

For upload, I figured out multipart usage with binary upload. I am passing p12 through the query param. My API is POST '/upload/',  and I am calling this using
curl -X POST --form key_file=@backup.p12 --form string_param_key=string_param_value http://localhost:3000/api/v1/upload?format=p12

Thank you for your help.
Deepak.

Daniel Doubrovkine

unread,
Jun 19, 2013, 8:29:32 AM6/19/13
to ruby-...@googlegroups.com
Right, GET /download will try to use whatever your default format is, but explicitly setting the content type in the code would work around that. By specifying the extension you're telling the API to use the application/x-pkcs12 content type.
Reply all
Reply to author
Forward
0 new messages