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

POST to API Endpoint has Empty Params #28564

Closed
uhrohraggy opened this issue Mar 24, 2017 · 3 comments
Closed

POST to API Endpoint has Empty Params #28564

uhrohraggy opened this issue Mar 24, 2017 · 3 comments

Comments

@uhrohraggy
Copy link

uhrohraggy commented Mar 24, 2017

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":"lkjlkj@kjhjk.com"}}}

Controller

private
      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

@vipulnsward
Copy link
Member

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.

@ngn01
Copy link

ngn01 commented Apr 10, 2017

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

@uhrohraggy
Copy link
Author

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

theasteve added a commit to theasteve/tyrion that referenced this issue Nov 2, 2020
Since we are using this Rails app as API only. There was an error
preventing the params to be access in the controller. The reason was
that due to the MIME type, the request was throwing a 500 error.

The fix was to register the type MIME `application/vnd.api+json` so that
`content_type` header has the correct type.

Another error preventing was the relationship constrain of `user_id`
unable to be `nil` for `topic.user_id` which was address in the
controller method `create`.

This was a temporary change for the `@topic.user_id = 1` which we will
change as soon as we are able to.

related:
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
* rails/rails#28564
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants