remove quotes from returned string of grape api
Asked Answered
C

4

7

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.

Contagious answered 7/5, 2015 at 1:52 Comment(0)
G
2

You don't need to use 'body', all that is left to do is to add one line in the API class (above this method):

content_type :txt, 'text/plain'

So that Grape uses :txt formatter for all endpoints that serve text/plain content

Gatian answered 6/12, 2016 at 3:15 Comment(0)
S
2

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.

Scuttlebutt answered 30/10, 2019 at 11:2 Comment(0)
R
1

According to this you can do the following:

class API < Grape::API
  get 'foo' do
    content_type 'text/plain'
    body 'hello world'
  end
end
Rummy answered 7/5, 2015 at 5:34 Comment(3)
It's not the single quotes that should do the trick, its body that should do it according to the documentation.Rummy
I previously included the incorrect URL. I've updated my answer with the URL I wanted to post, but apparently that didn't work out for you. There's a lot of documentation on that page btw.Rummy
I have the same problem. Any progress on this? Why is such a simple thing so complicated?Sulla
S
1

Using content_type :txt, 'text/plain' above the method and using the body method worked for me. Here is my code snippet:

content_type :txt, "text/plain" desc "ping pong" get "/ping" do challenge = params["hub.challenge"] challenge = "pong" if challenge.to_s.empty? status 200 body challenge end

Sulla answered 3/1, 2017 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.