Jsonapi-resources: "The required parameter, data, is missing" ..but it's not actually missing.

Created on 26 Sep 2016  路  3Comments  路  Source: cerebris/jsonapi-resources

This is a strange one.

My error message is
{"errors":[{"title":"Missing Parameter","detail":"The required parameter, data, is missing.","code":"106","status":"400"}]}

My request was:

{"data":{"type":"income-items", "attributes":{"name":"","amount":"","frequency":1,"nextDueAt":""},"relationships":{"worksheet":{"data":{"id":"13","type":"worksheets"}}}}}

The rails console reports:

Processing by Api::V1::IncomeItemsController#create as */*
19:53:41 web.1    |   Parameters: {"income_item"=>{}}
19:53:41 web.1    | Completed 400 Bad Request in 2ms (Views: 0.2ms | ActiveRecord: 0.0ms)

No idea how it thinks the data attribute is missing.

The rails logger is giving me this;

22:20:55 web.1 | Started POST "/api/v1/income-items" for ::1 at 2016-09-29 22:20:55 +0930
22:20:55 web.1 | Processing by Api::V1::IncomeItemsController#create as JSON
22:20:55 web.1 | Parameters: {"income_item"=>{}}

Any ideas what's going on?

Most helpful comment

Ok managed to get this resolved by jumping on the project gitter and got some fantastic help from @lgebhardt.

The short version is that JR declares the mime type itself and does fancy stuff with that to define the payload. If you re-declare the mime type anywhere yourself it breaks things and the data attribute doesn't seem to load and JR gets a bit lost.

How to solve!

Remove all references to application/vnd.api+json that may be explicitly declared anywhere in your app.

For me it meant removing code in initializers/mime-type.rb that I added when I implemented ActiveModelSerializer and left in there 'just in case' when I was transitioning to JR.

And it also meant removing this from my initializers/jsonapi-resources which I added at some point for good measure when I couldn't get things working.

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

Once this started going I also found that this config setting made no difference to make it work or not, so if you're attempting to play with this it probably won't help.

config.raise_if_parameters_not_allowed = true # didn't change anything for me.

All 3 comments

Ok managed to get this resolved by jumping on the project gitter and got some fantastic help from @lgebhardt.

The short version is that JR declares the mime type itself and does fancy stuff with that to define the payload. If you re-declare the mime type anywhere yourself it breaks things and the data attribute doesn't seem to load and JR gets a bit lost.

How to solve!

Remove all references to application/vnd.api+json that may be explicitly declared anywhere in your app.

For me it meant removing code in initializers/mime-type.rb that I added when I implemented ActiveModelSerializer and left in there 'just in case' when I was transitioning to JR.

And it also meant removing this from my initializers/jsonapi-resources which I added at some point for good measure when I couldn't get things working.

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

Once this started going I also found that this config setting made no difference to make it work or not, so if you're attempting to play with this it probably won't help.

config.raise_if_parameters_not_allowed = true # didn't change anything for me.

My problem was because of a conflict caused by the jsonapi-rails gem. For MIME types of application/vnd.api+json, this gem rewrites the request parameters hash so that the first key is _jsonapi rather than data. Removing this gem fixed the "required parameter, data, is missing" error for me.

Was this page helpful?
0 / 5 - 0 ratings