-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hard to return preformatted json with application/json content-type #1989
Comments
I think in your case a custom formatter for JSON is the right answer. You don't have pre-built JSON, you have a string. Grape can't guess that it's JSON. And the JSON formatter will translate a string into ... JSON. |
My issue is that even if I explicitly set a :txt or :binary formatter, it still applies :json formatter when I set Setting a content_type or header should not overwrite formatter. This: env['api.format'] = :binary # or :txt
content_type 'application/json' # or header 'Content-Type', 'application/json'
{key: 123}.to_json outputs |
You're sending JSON, not binary. So you should just be replacing the default JSON formatter with yours with |
It works, I just think that there should be a way to specify 'application/json' header without grape applying :json formatter.
|
Even if I do this: env['api.format'] = :txt
content_type 'application/json'
body {key: 123}.to_json grape still applies :json formatter |
This should work as is, and we have specs for this. I think something else is going on. Turn it onto a spec or a small repro of some kind? Maybe add an example to https://github.com/ruby-grape/grape-on-rack? |
I'm having exactly the same issue. If this is used: env['api.format'] = :binary # or :txt
content_type 'application/json' # or header 'Content-Type', 'application/json'
{key: 123}.to_json Then it still calls the JSON formatter. If the |
Here is an example using grape-on-rack: https://github.com/waynerobinson/grape-on-rack Please take a look at the extra methods added to The only one that doesn't call the JSON formatter is the one where the content type is set to |
This does look like a bug, and shouldn't have been closed. Let's fix it in #2171. |
For example, we have the following endpoint, returning preformatted json (I return json right from the database)
Here's what will be returned:
"{\"key\":123}"
withapplication/json
content-typeGrape applies additional
.to_json
call.Now let's try to fix this problem, taking into assumption that I already have a prebuilt JSON in my endpoint.
Options:
Adding
env['api.format'] = :binary
or:txt
—{"key":123}
but empty content-typeAdding
content_type 'application/json'
orheader 'Content-Type', 'application/json'
to :txt or :binary format —"{\"key\":123}"
The only way I was able to solve this is by overwriting :json formatter with:
Maybe there's another way?
The text was updated successfully, but these errors were encountered: