Hello!
I am confused about formats and content-types, and how different endpoints can respond with different kinds of data.
Here's my situation: my API has three endpoints. Two should return PDF data and one should return JSON data:
class API < Grape::API
params do
# a few HTTP params
end
post :foos
# generates a PDF
end
params do
# a few HTTP params
end
post :bars
# generates a PDF
end
get :bazs
# returns Baz instances' attributes as JSON
end
end
The only way I can get this to work is:
class API < Grape::API
content_type :pdf, 'application/pdf'
format :pdf
# :foos stuff as above
# :bars stuff as above
get :bazs
content_type 'application/json'
some_objects.to_json
end
end
In other words, make PDF the default then crowbar /bazs into returning JSON by overriding its content type and manually JSONifying my objects.
Is that the best way to do it? It would be nice to do this instead but it won't work for me:
get :bazs
format :json
some_objects
end
Thanks in advance!
Yours,
Andy Stewart