jsonapi.org and custom vendor content type

131 views
Skip to first unread message

Igor Kapkov

unread,
Sep 1, 2015, 9:09:36 PM9/1/15
to Grape Framework Discussion
Hi everyone,


What I need to do is accept only "application/vnd.api+json" and response with "application/vnd.api+json" Content-Type.

Daniel (dblock) suggest to add "format :jsonapi" to existing

content_type :jsonapi, "application/vnd.api+json" 
formatter :jsonapi, -> (object, _env) { Oj.dump(object) }

But it's totally different behaviour, now it's start answering to "application/json" or "text/plain" with "application/vnd.api+json" instead of return "406 Not Acceptable".

Any ideas?

Thanks.

Daniel Doubrovkine

unread,
Sep 2, 2015, 8:45:23 AM9/2/15
to ruby-...@googlegroups.com
Grape actually supports this out of the box, see https://github.com/ruby-grape/grape/pull/475 where it was added.

Lets backup a step, what exactly is not working. Care to provide an example?

--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Igor Kapkov

unread,
Sep 2, 2015, 10:40:36 PM9/2/15
to Grape Framework Discussion
Yep, but later jsonapi content-type was removed.

It's not exactly not working. I can set content-type in `before` but it's strange for me. I don't understand why if I do some vendoring grape should strip "vnd.something+" from content type. Or if I add format it adds fallbacks, I read docs and source and can't find combination of: content_type, formatter, format, default_format, default_error_formatter to work as I need.

I need to accept and response only "application/vnd.api+json". For all other return 406 errors. Here is quick simple example https://github.com/igas/jsonapi-grape-example

Thanks.

Daniel Doubrovkine

unread,
Sep 3, 2015, 8:34:01 AM9/3/15
to ruby-...@googlegroups.com
I think the best solution is to check the content-type in a before filter, see https://github.com/igas/jsonapi-grape-example/pull/1.

There're two parts: Content-Type of the data sent and Accept header for content version/format negotiation.

By default grape accepts all content-types that you throw at it, so you can do forms, JSON, whatever. If it has a parser it will parse the body and offer the API the data inside params. A lot of this behavior is implemented in Rack, not even in Grape, so I think the best solution is to just shortcut it. I did it in a before block. 

The accept side of content-negotiation is http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1, that's much more involved. Grape versioning supports versioned content-negotiation and it seems like you don't want any of the versioning to happen here, so you can simply ignore this. 

If I am missing a scenario, add a spec that fails to your example and lets try to iterate on it until all the issues are resolved. I am not against supporting such behavior out of the box in Grape, we'll have to see what the DSL looks like.

Reply all
Reply to author
Forward
0 new messages