rails: Sending JSON in functional tests does not populate the params hash

test 'sending raw JSON does not populate the params hash' do
  @request.headers['Content-Type'] = 'application/json'
  post :create, { key: "value" }.to_json

  assert_equal "value", params['key'] # => Will fail!
end

will result in following params hash:

{"controller"=>"posts", "action"=>"create"} # Hence the missing key-value pair.

However sending the corresponding request with curl or other apps, it works like intended. I even asked on StackOverflow, however no answer yet. Looks like a bug for me.

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 31 (23 by maintainers)

Commits related to this issue

Most upvoted comments

In case anyone end up here via Google (like I did), the correct way to do this in Rails 5 is:

post new_widget_url, as: :json, params: { foo: "bar" }
# will set the correct content-type header automatically

this is only marginally related to the question, but hopefully helpful to others with the same question as I had

I started to test with Rack-test. It is a million times faster, no magic and does exactly what you would think it does.