使用HTTPie将新用户发布到Rails API时遇到问题

时间:2017-02-03 20:43:51

标签: ruby-on-rails httpie

我尝试使用HTTPie测试我的Rails 5 API端点。我在终端中运行的命令是:

http POST :3000/users email=test@example.com password=anewpassword password_confirmation=anewpassword

我得到的回应是:

HTTP/1.1 400 Bad Request
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Request-Id: c3ca4d50-9501-46e9-a7e1-a4576d6cdd7e
X-Runtime: 0.010404
X-XSS-Protection: 1; mode=block

{
  "errors": [
    "Password can't be blank"
  ]
}

这意味着它违反了我的验证。但是,据我所知,它不应该是。这是我从Rails服务器终端获得的:

Started POST "/users" for 127.0.0.1 at 2017-02-03 12:09:54 -0800
Processing by UsersController#create as JSON
  Parameters: {"email"=>"test@example.com", "password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]", "user"=>{"email"=>"test@example.com"}}
(0.1ms)  BEGIN User Exists (0.4ms)  SELECT  1 AS one FROM "users" 
WHERE LOWER("users"."email") = LOWER($1) LIMIT $2  [["email","test@example.com"], ["LIMIT", 1]]
(0.1ms)  ROLLBACK
Completed 400 Bad Request in 3ms (Views: 0.1ms | ActiveRecord: 0.6ms)

关于我做错了什么的任何想法?我有一种感觉,我没有正确使用HTTPie,但我对Google-fu不太了解。

修改

我可以确认路线是否有效。使用Postman并在请求正文中传递以下JSON获得预期的响应:

{"user": {"email": "test@example.com","password": "anewpassword","password_confirmation": "anewpassword"}}

Response: 
    {"status": "User created successfully"}

1 个答案:

答案 0 :(得分:0)

在这篇文章(Sending nested JSON object using HTTPie)的帮助下,我修改了我的HTTPie请求,如下所示:

http POST :3000/users user:='{"email": "test@example.com", "password": "anewpassword", "password_confirmation": "anewpassword"}'

这给了我预期的回应:

HTTP/1.1 201 Created
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/json; charset=utf-8
ETag: W/"8a33506505f34984dfa0c1fe7e8a9ef3"
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Request-Id: 6e34a81d-e5cb-4109-98ca-b406c962189b
X-Runtime: 0.196269
X-XSS-Protection: 1; mode=block

{
    "status": "User created successfully"
}