Pregunta

I'm using rails4 + rspec 3. I want to make HTTP calls, and pass both params (such as JSON body or query string), and also HTTP headers. I was able to pass one of these two, but not both.

when I try something like:

post api_v1_post_path(@myid), {} , {"X-Some-Header" => "MyValue"} 

it works fine and the headers fine, but if I do something like:

post api_v1_post_path(@myid), {"myparam" => "myvalue"} , {"X-Some-Header" => "MyValue"} 

I get the following error:

Failure/Error: post api_v1_post_path(@myid), {"myparam" =>"myvalue"}, headers
 ActionDispatch::ParamsParser::ParseError:
   795: unexpected token at 'myparam'

Any ideas?

¿Fue útil?

Solución

It seems that the POST params are expected to be JSON encoded. 795: unexpected token at 'myparam' is caused when the app tries to JSON decode the params that are not encoded.

Use .to_json with the post params.

post api_v1_post_path(@myid), {"myparam" => "myvalue"}.to_json , {"X-Some-Header" => "MyValue"}

You may want to use let:

describe 'Test' do
  let( :params  ){{ myparam: 'myvalue' }}
  let( :headers ){{ 'X-Some-Header' => 'MyValue' }}

  it 'succeeds' do
    post api_v1_post_path(@myid), params.to_json , headers
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top