Testing POST ActionDispatch::Http::Parameters::ParseError: 765

瘦欲@ 提交于 2019-12-21 04:05:11

问题


It says there's an unexpected token in my params.

"ActionDispatch::Http::Parameters::ParseError: 765: unexpected token at 'conversation_identifier[participant_list][]=2&conversation_identifier[participant_list][]=1"

A version of the test with magic numbers for clarity:

let(:headers) do
  { 'HTTP_CURRENT_USER_ID' => 2,
    'Content-Type'         => 'application/json' }
end
let(:params) { { conversation_identifier: { participant_list: [1, 2] } }

it 'is getting testy' do
  post resource_url, params: params, headers: headers
  assert_equal 201, response.status
end

Now here's what's weird. It has no trouble parsing those params if I give it no headers.


回答1:


Removing the 'Content-Type' => 'application/json' solved the problem.

Finally remembered that ActionDispatch uses the headers to know how to parse the params. 'Content-Type' => 'application/json' is a standard piece of boilerplate that's ok to throw around with GET requests and query params but not with POST when used this way.



来源:https://stackoverflow.com/questions/49053954/testing-post-actiondispatchhttpparametersparseerror-765

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!