0

我有一个奇怪的情况,邮递员或卷曲的帖子工作得很好,例如下面的工作按预期工作并创建一个新记录:

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"

但是当我尝试通过如下的 RSpec 请求规范访问它时,这给了我一个状态 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

你遇到过这个问题吗?我错过了什么明显的东西?非常欢迎任何反馈。

4

1 回答 1

0

感谢@JollyProgrammer,将参数转换为 json 就可以了:

post api_v1_drivers_path, params: {
   data: {
     type: 'drivers',
     attributes: {
       firstname: 'John',
       lastname: 'Doe',
       age: 24,
       status: 'pending'
     }
   }
}, as: :json, headers: headers
于 2018-03-22T13:47:37.753 回答