How to allow Binary File download using GRAPE API

后端 未结 2 837
抹茶落季
抹茶落季 2021-02-04 15:21

I want to allow downloading a binary file (.p12 file) using ruby\'s Grape API. This is what I am trying.

get \'/download_file\' do
  pkcs12 = generate_pkcsfile 
         


        
2条回答
  •  情话喂你
    2021-02-04 15:59

    I think your Grape code is OK, I have tested a variant of it using a browser and a Mac HTTP tool (called GraphicalHTTPClient) that I use to test APIs. I successfully loaded a binary file from disk and transferred it with MIME type 'application/octet-stream' using almost identical code to yours:

      get :download do
        data = File.open('binary_data').read
        content_type 'application/octet-stream'
        body data
      end
    

    I suggest your problem is with the API client and/or the character encoding (as suggested by Stuart M). Although another possibility that occurs to me form our discussion so far, is that some Rack middleware is being triggered incorrectly, and modifying the output from Grape.

提交回复
热议问题