Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove quotes from returned string of grape api

I want to return raw data/blob from my grape/rest api.

I followed the thread at : https://github.com/intridea/grape/issues/412

for a code like :

get 'foo' do
  content_type 'text/plain'
  "hello world"
end

1) I used : format 'txt' - I got quoted text like : "hello world" no error on the browser though, curl gives Content-Type: text/plain but the quotes are not removed

2) env['api.format'] = :txt gives error in browser

3) content_type :txt, 'text/plain' gives error in browser wrong number of args

Any other ways to fix this ?

Thanks.

like image 432
resultsway Avatar asked May 07 '15 01:05

resultsway


2 Answers

Here's what worked for me:

get 'foo' do
  content_type 'text/plain'
  env['api.format'] = :binary
  body 'Stuff here'
end

The documentation says:

env['api.format'] = :binary # there's no formatter for :binary, data will be returned "as is"

So as long as you don't override the :binary formatter, you should be fine.

like image 150
Francois Avatar answered Oct 14 '22 02:10

Francois


According to this you can do the following:

class API < Grape::API
  get 'foo' do
    content_type 'text/plain'
    body 'hello world'
  end
end
like image 1
Kevin Avatar answered Oct 14 '22 02:10

Kevin