Search code examples
ruby-on-railshttpie

Having trouble POSTing new user to Rails API with HTTPie


I'm trying to test my Rails 5 API endpoints using HTTPie. The command I run in Terminal is:

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

The response I get is:

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"
  ]
}

Which means it's bumping up against my validations. However, from what I can tell it shouldn't be. This is what I get from the Rails server terminal:

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)

Any ideas as to what I'm doing wrong? I have a feeling I'm not using HTTPie correctly, but I can't find much with my Google-fu.

EDIT

I can confirm the route is working. Using Postman and passing the following JSON in the request body gets the expected response:

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

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

Solution

  • With the help of this post(Sending nested JSON object using HTTPie) I modified my HTTPie request to look like this:

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

    Which gave me the expected response of:

    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"
    }