Rails: POST to API Endpoint has Empty Params

Created on 24 Mar 2017  路  3Comments  路  Source: rails/rails

Params is set but the request payload is being lost.

[1] pry(#<Api::RegistrationsController>)> params
=> <ActionController::Parameters {"format"=>"json", "controller"=>"api/registrations", "action"=>"create"} permitted: false>
[2] pry(#<Api::RegistrationsController>)> params.require(:data)
ActionController::ParameterMissing: param is missing or the value is empty: data
Request Headers

Request Headers

Host: test.lvh.me:3000
Connection: keep-alive
Content-Length: 1038
Accept: application/vnd.api+json
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Origin: http://test.lvh.me:4200
Content-Type: application/vnd.api+json
Referer: http://test.lvh.me:4200/register/individual-academics
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

Raw Payload
{"data":{"attributes":{"first-name":"Test","last-name":"User","email":"[email protected]"}}}

Controller

      def registration_params
        params.require(:data).permit({
          attributes: [
            'first-name',
            'last-name',
            'email',
            ]
        })
      end

If I set up the same app using Rails 4.2.6/Ruby 2.3.3, this works perfectly with no changes in the payload, so it looks like it's something with the way Rails 5 handles requests, potentially?

I should also mention that the source URL is test.lvh.me:4200 (an ember app) to the Rails API test.lvh.me:3000, so CORS may play a role here in stripping the params? I'm not receiving any error as the headers appear correct and CORS is set up here to allow any origin via Rack::Cors middleware.

Rails version:
Rails 5.0.1
Ruby version:
Ruby 2.3.3

actionpack needs work

Most helpful comment

You need to add the following line to config/initializers/mime_types.rb
Mime::Type.register "application/vnd.api+json", :json

All 3 comments

Hi there, please create an executable failing test case using https://github.com/rails/rails/blob/master/guides/bug_report_templates/action_controller_gem.rb as a template so that others can reproduce and help you solve the issue.

You need to add the following line to config/initializers/mime_types.rb
Mime::Type.register "application/vnd.api+json", :json

Just wanted to follow up @vipulnsward and @ngn01 really appreciate this response. Worked successfully, and helped me understand the relationships among everything.

Was this page helpful?
0 / 5 - 0 ratings