Rspec-rails: Unable to POST raw request body

Created on 15 Dec 2017  路  9Comments  路  Source: rspec/rspec-rails

Here's a snippet from my spec:

foo = "some raw data"
post "/foo", foo

I am getting an ArgumentError, despite the Relish docs saying this is ok.

Ready to go

Most helpful comment

For reference, I'm using body in the post like so to set the RAW_POST_DATA.

 post :event, body: params.to_json

This is coming from ActionPack in the process which powers post etc: https://github.com/rails/rails/blob/v5.2.2/actionpack/lib/action_controller/test_case.rb#L477-L479

All 9 comments

Could reproduce

require 'rails_helper'

RSpec.describe "logout", type: :controller do
  it "removes the cookie" do
    foo = "some raw data"
    post "/logout", foo
  end
end

(master versions of all rspec repos, latest rails, both controller and request specs)

This is reproducible back to at least RSpec 3.5

Also @request.env['RAW_POST_DATA'] = 'some raw data' did not work.
After the POST request, the ENV 'RAW_POST_DATA' is empty

Here is some snippet:
In Spec file:
@request.env['RAW_POST_DATA'] = 'some raw data'
post :some_action

In Rails Controller:
request.body.read => "" # should be 'some raw data'

It is braking with Rails 5.2.1

For reference, I'm using body in the post like so to set the RAW_POST_DATA.

 post :event, body: params.to_json

This is coming from ActionPack in the process which powers post etc: https://github.com/rails/rails/blob/v5.2.2/actionpack/lib/action_controller/test_case.rb#L477-L479

    Failure/Error: post '/rewards', headers: {}, body: str, format: :text

     ArgumentError:
       unknown keywords: body, format

what it's?

use params: str without headers.

If you pass json then it consider as raw body and if you pass hash then it consider as params.

post :event, params.to_json

I think this issue solves it, or at least it did for me -> https://github.com/rspec/rspec-rails/issues/1650

This depends on your rails versions and is no longer in the docs, closing for now

Was this page helpful?
0 / 5 - 0 ratings