我的问题与Rails 5 API POST Create params empty非常相似,但答案并没有帮助。 Params已设置但请求有效负载正在丢失。
[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
请求标题
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
原始有效负载
{"data":{"attributes":{"first-name":"Test","last-name":"User","email":"lkjlkj@kjhjk.com"}}}
更新 如果我使用Rails 4设置相同的应用程序,这完全没有有效负载的变化,所以它看起来像是Rails 5处理请求的方式,可能吗?
更新2
ActiveSupport::JSON.decode(request.body.string)
正确解析为哈希值,但未包含在params中。我当然可以做params.merge!但这感觉就像一个糟糕的电话。
request.format是:#<Mime::Type:0x007f89b6c63df0 @hash=2978300210816280263, @string="application/json", @symbol=:json, @synonyms=["text/x-json", "application/jsonrequest"]>
答案 0 :(得分:2)
要解决此问题,您需要将以下行添加到config/initializers/mime_types.rb
:
Mime::Type.register "application/vnd.api+json", :json