Kingsley Ijomah
Kingsley Ijomah

Reputation: 3403

Getting a status 400 (BadRequest) with jsonapi(0.9.0) on Rails 5 using Rspec-rails(3.0) request spec

I have a strange situation where a postman or curl post, work just fine, e.g below works as expected and creates a new record:

curl -X POST \                                   
  -H "Content-Type: application/vnd.api+json" \
  -H "Accept: application/vnd.api+json" \
  -d '{
  "data": {
    "type": "drivers",
    "attributes": {
      "firstname": "John",
      "lastname": "Doe",
      "age": "24",
      "status": "pending"
    }
  }
}' "http://localhost:3000/api/v1/drivers"

but when I try to get to it via RSpec request spec as below, this gives me a status 400 (BadRequest):

...
describe "POST /api/v1/drivers" do
  context 'valid data' do 
    it 'saves valid data' do
      headers = { 
        "Content-Type" => "application/vnd.api+json",
        "Accept" => "application/vnd.api+json " 
      }

      post api_v1_drivers_path, params: {
        data: {
          type: 'drivers',
          attributes: {
            firstname: 'John',
            lastname: 'Doe',
            age: 24,
            status: 'pending'
          }
        }
      }, headers: headers

      expect(response).to have_http_status(201)
    end
  end
end

Have you come across this issue? something obvious I am missing? any feedback is very welcome.

Upvotes: 0

Views: 551

Answers (1)

Kingsley Ijomah
Kingsley Ijomah

Reputation: 3403

converting params to json did the trick, thanks to @JollyProgrammer:

post api_v1_drivers_path, params: {
   data: {
     type: 'drivers',
     attributes: {
       firstname: 'John',
       lastname: 'Doe',
       age: 24,
       status: 'pending'
     }
   }
}, as: :json, headers: headers

Upvotes: 0

Related Questions