Skip to content

Instantly share code, notes, and snippets.

@nlsrchtr
Created March 12, 2018 21:07
Show Gist options
  • Save nlsrchtr/966fcea38c53b29b4fff7bcdb5543d42 to your computer and use it in GitHub Desktop.
Save nlsrchtr/966fcea38c53b29b4fff7bcdb5543d42 to your computer and use it in GitHub Desktop.

Thanks for the development of this gem, it saved me a lot of time!

Since I would like to follow the JSON API conventions as much as possible, I'm wondering how I can switch the content-type completely to "application/vnd.api+json".

Doing this for the responses, I'm using:

before do
  content_type("application/vnd.api+json")
end

But I didn't found any proper way of setting it for the incoming requests. If clients are using this content-type, they are getting block with "The requested content-type 'application/vnd.api+json' is not supported.".

Looking into grape itself, I found that support was initially added and later removed because if wasn't fully supported, I think the error response was not rendered correctly.

Now I'm wondering if the roar-jsonapi gem is offering any help in this? Is there any formater, I could use to define my own jsonapi content-type with something like this:

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

Because this example generates a "stack level" to deep, when rendering the response, but works with accepting the request.

I would really appreciate any help.

@nlsrchtr
Copy link
Author

My current implementation looks like

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

This works, because I'm using roar-jsonapi, but I'm wondering if there is a better alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment