Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 685
Adamantish Avatar asked Mar 01 '18 16:03

Adamantish


1 Answers

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.

like image 127
Adamantish Avatar answered Feb 21 '23 21:02

Adamantish