1

我正在尝试使用 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"}
4

1 回答 1

0

在这篇文章(使用 HTTPie 发送嵌套 JSON 对象)的帮助下,我修改了我的 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"
}
于 2017-02-03T21:17:58.737 回答