Ruby-on-rails – How to set request headers in rspec request spec


In the controller spec, I can set http accept header like this:

request.accept = "application/json"

but in the request spec, "request" object is nil. So how can I do it here?

The reason I want to set http accept header to json is so I can do this:

get '/my/path'

instead of this

get '/my/path.json'

Best Solution

You should be able to specify HTTP headers as the third argument to your get() method as described here:

and here

So, you can try something like this:

get '/my/path', nil, {'HTTP_ACCEPT' => "application/json"}