Skip to content
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

Adding custom mime type for 'application/vnd.api+json' seems to be required to receive data from clients using JSON API #1027

Closed
begedin opened this issue Jul 30, 2015 · 18 comments

Comments

@begedin
Copy link

begedin commented Jul 30, 2015

Using ember and ember-data with it's built in JSON API support means my client is sending POST/PUT/PATCH requests with ContentType set to 'application/vnd.api+json'.

By default, even though I've set my adapter in my rails app to:json_api, the controller action for create/update doesn't receive any data.

After some troubleshooting and googling, I've found out I need to create an initializer to set the rails application to interpret 'application/vnd.api+json' as json:

ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime::Type.lookup('application/vnd.api+json')]=lambda do |body|
  JSON.parse(body)
end

Once this was added, I'm receiving the data as expected.

The issue here is, i'm not sure if this is what I'm supposed to be doing.

  1. Will AMS at some point do this, or something similar internally?
  2. Should I do it this way?
  3. Should I do it some other, more proper way?
  4. Did I miss something altogether? Is it well documented already and I actually just didn't set some property somewhere?

If it's one of the first 3, I'm thinking it should be documented in some way.

@joaomdmoura
Copy link
Member

Hey @begedin, thank you for the issue:

1 - Will AMS at some point do this, or something similar internally?

Probably, you might not even need to set the adapter depending on how we decide to do this.

2 - Should I do it this way?

Not sure, we haven't started to implement it so it's a new problema for us as well, but we are already working on deserialization, what means we will face this problem soon.

3 - Should I do it some other, more proper way?

As I said, I'm not sure, we will face the same problem on the deserialization implementation the next days, so might find another way but you're ahead of us for now 😄

4 - Did I miss something altogether? Is it well documented already and I actually just didn't set some property somewhere?

No, we don't support it yet.

I'll close this one for now but will keep the dicussion opened, mostly because I want to give updates base on the implementation that we will work on! Also, @begedin, let us know in case you have any problem with this, I might open the issue again and we will definitely help you 😄

@shicholas
Copy link
Contributor

hey @begedin I'm using rails with AMS .10 and the latest ember/ember-data (1.13). I added this as an initializer (e.g. config/initializers/register_json_api_mime_type.rb in your rails app):

api_mime_types = %W(
  application/vnd.api+json
  text/x-json
  application/json
)

Mime::Type.unregister :json
Mime::Type.register 'application/json', :json, api_mime_types

and everything seems to be working fine :).

@begedin
Copy link
Author

begedin commented Jul 31, 2015

@shicholas That seems like a cleaner way to do it, so thank you for that snippet.

jumski added a commit to jumski/blog-backend that referenced this issue Sep 4, 2015
get rid of `ApplicationController#determine_format` hack.
More info:
rails-api/active_model_serializers#1027

Really dunno why it was working with `determine_format` until
`active_model_serializers` were updated to master,
but not worth digging this on friday evening :)
jumski added a commit to jumski/blog-backend that referenced this issue Sep 7, 2015
get rid of `ApplicationController#determine_format` hack.
More info:
rails-api/active_model_serializers#1027

Really dunno why it was working with `determine_format` until
`active_model_serializers` were updated to master,
but not worth digging this on friday evening :)
jumski added a commit to jumski/blog-backend that referenced this issue Sep 7, 2015
get rid of `ApplicationController#determine_format` hack.
More info:
rails-api/active_model_serializers#1027

Really dunno why it was working with `determine_format` until
`active_model_serializers` were updated to master,
but not worth digging this on friday evening :)
@jbonhag
Copy link

jbonhag commented Sep 30, 2015

@shicholas Thanks, that was a great help.

@wjdhamilton
Copy link

@shicholas Thank you very much!

@jurgenwerk
Copy link

@joaomdmoura any news on when we won't have to declare custom MIME type manually for when json_api adapter is selected?

@bf4
Copy link
Member

bf4 commented Jan 13, 2016

@matixmatix right now that change would have to be in the Rails JSON Renderer, if I'm not mistaken. or do you mean something like #1027 (comment)

rashkov added a commit to rashkov/bloggins that referenced this issue Jan 30, 2016
Was not receiving any data in my update method.
Found a solution here:
rails-api/active_model_serializers#1027
Need to support the jsonapi mime type application/vnd.api+json
for params parser to work properly.
@halorium
Copy link

Thank you @rashkov.

Mime::Type.unregister :json
Mime::Type.register "application/json", :json, %w( text/x-json application/json application/vnd.api+json )
ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime::Type.lookup('application/vnd.api+json')]=lambda do |body|
JSON.parse(body)
end

This solved my issue.

@bf4
Copy link
Member

bf4 commented Mar 10, 2016

@boobooninja this solution is technically incorrect, even though it works fine.

I basically just removed this pattern from Rails 5.

Better would be to register a jsonapi renderer

@vasilakisfil
Copy link
Contributor

vasilakisfil commented May 20, 2016

@bf4 could you elaborate ? Could you give a dirty but correct solution ?

I use 0.10.0.rc5 with JSONAPI and I had the same issue. It can be VERY furstrating I thing I am lucky that I found this issue in the first 30 mins. What it needs to be done so that users don't have this issue ?

@bf4
Copy link
Member

bf4 commented May 20, 2016

ActiveSupport.on_load :action_controller do
require 'active_model_serializers/register_jsonapi_some.....
End

Sorry, on phone

If you can make a pr summarizing what brought you here and what the solution is, that would be great.

Maybe see http://www.benjaminfleischer.com/journey-of-a-media-type-in-rails-part-1 re content negotiation

B mobile phone

On May 20, 2016, at 3:19 PM, Filippos Vasilakis notifications@github.com wrote:

@bf4 could you elaborate ? Could you give a dirty solution ?

I use 0.10.0.rc5 with JSONAPI and I had the same issue. It can be VERY furstrating I thing I am lucky that I found this issue in the first 30 mins. What it needs to be done so that users don't have this issue ?


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub

rdavila pushed a commit to gitlabhq/gitlabhq that referenced this issue Sep 13, 2016
Fix two problematic bits of code that will be deprecated or broken in Rails 5.

Found in the Rails 5 MR: !5555

These are safe to use in Rails 4.2.7 as well as Rails 5.0.0, so I figured I'd backport them for the sake of making that merge request smaller.

The explanation for the mime_types.rb code is here: rails-api/active_model_serializers#1027 (comment)

See merge request !6214
@rmcsharry
Copy link

I am using AMS 0.10.2 with Ember 2.7 and did not need to register a mime type for everything to work. I simply added the AMS gem and configured it as recommended:

/config/initializers/active_model_serializer.rb

require 'active_model_serializers/register_jsonapi_renderer'

ActiveModelSerializers.config.adapter = :json_api

However, the Header being sent from Rails is application/json and not application/vnd.api+json

My understanding is that if you want the api to be spec-compliant, the server should respond with the latter mime-type.

In order to achieve that, then you need to register the correct mime-type, even though it seems Ember and the API work without doing so.

api_mime_types = %W(
  application/vnd.api+json
  text/x-json
  application/json
)
Mime::Type.register 'application/vnd.api+json', :json, api_mime_types

Is there some mismatch going on here or am I just not groking something here?
It seems strange to have to need two initialisers.

@richmolj
Copy link
Contributor

richmolj commented Sep 16, 2016

@rmcsharry are you using format.jsonapi or render :jsonapi? I believe that will give you the correct response header without the additional initializer. See https://github.com/rails-api/active_model_serializers/blob/master/docs/integrations/ember-and-json-api.md.

You shouldn't need to register the mime type yourself anymore, if you are using require 'active_model_serializers/register_jsonapi_renderer'. The code does that for you and this line should render the correct header.

If that helps, I suggest we close this issue (as much of the discussion is dated and now misleading) and maybe make that section of the documentation more obvious.

@allthesignals
Copy link
Contributor

allthesignals commented Jan 20, 2017

I think I'm having this exact issue. I'm using AMS 0.10.4 with Rails 4.2.

Testing in postman, it only seems to accept application/json. When Content-Type is application/vnd.api+json, it throws the error stated above:
screen shot 2017-01-20 at 6 33 54 pm

Is there something obvious I'm missing? Maybe there's another gem that's interfering. I did include the special initializer mentioned in the ember README.

EDIT:
Fixed it, I had the include, but I was missing inheriting from ActionController::API.

@bf4
Copy link
Member

bf4 commented Jan 21, 2017

@allthesignals I have a PR open in Rails to return a 415 when a mime type isn't registered. There's a lot of misinformation on the web. Register vnd.api+json as its own media type. AMS provides a way to do this for you.

backspace added a commit to backspace/waydowntown-server that referenced this issue Dec 1, 2019
It’ll surely turn out that storing a single integer result
for each game is too limited, but I’ll address that when
it becomes necessary.

Thanks to @shicholas for the MIME type pointer here:
rails-api/active_model_serializers#1027 (comment)
@digaoddc
Copy link

digaoddc commented Nov 20, 2020

To enable the mime type application/vnd.api+json in the gem version >= v0.10.1, do this:

Add this to your controller:
require 'active_model_serializers/register_jsonapi_renderer'

And use the type json_api in your render:

def show
  resource = Resource.find(params[:id])
  render jsonapi: resource
end

And then in the response, you'll see the header content-type with application/vnd.api+json

References:
#1747 - Improve jsonapi mime type registration for Rails 5 (@remear)
https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/http/mime_types.rb
http://www.seanbehan.com/how-to-get-a-list-of-all-registered-mime-types-in-rails/

@jramiresbrito
Copy link

hey @begedin I'm using rails with AMS .10 and the latest ember/ember-data (1.13). I added this as an initializer (e.g. config/initializers/register_json_api_mime_type.rb in your rails app):

api_mime_types = %W(
  application/vnd.api+json
  text/x-json
  application/json
)

Mime::Type.unregister :json
Mime::Type.register 'application/json', :json, api_mime_types

and everything seems to be working fine :).

This solved for me. Thank you!

@bf4
Copy link
Member

bf4 commented Feb 16, 2021

@jramiresbrito I'm glad that worked for you. Please be aware we do not recommend that and provide a solution. In the future, when commenting on (years) old resolved threads, it is a good practice to include in your comment what new information you are adding which justifies the thread necromancy. I'm locking this thread now since there's ample references in it to justify it being closed, IMHO.

@rails-api rails-api locked as resolved and limited conversation to collaborators Feb 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests