使用POST无法使用CURL在Rails中登录

时间:2015-07-29 14:51:14

标签: ruby-on-rails curl

我在使用CURL和POST方法登录rails时遇到问题。我在终端输入的内容是

curl -H' Content-Type:application / json' -X POST http://localhost:3000/users/sign_in -d' {" user":{" email":" admin@admin.com",&#34 ;密码":" abc12345"}}'

但它在终端中返回了某种html / javascript文件。这是我的注册控制器代码:

class RegistrationsController < Devise::RegistrationsController

    private

    def sign_up_params 
        params.require(:user).permit(:name, :email, :password)
    end

    def account_update_params 
        params.require(:user).permit(:name, :email, :password, :current_password)
    end

end

这是我在CURL请求的终端中得到的结果:

http://localhost:3000/users/sign_in
Hostname was NOT found in DNS cache
Trying ::1...
Connected to localhost (::1) port 3000 (#0)
POST /users/sign_in HTTP/1.1
User-Agent: curl/7.37.1
Host: localhost:3000
Accept: application/json
Content-type: application/json
Content-Length: 58

upload completely sent off: 58 out of 58 bytes
HTTP/1.1 422 Unprocessable Entity 
Content-Type: text/html; charset=utf-8
Content-Length: 107406
X-Request-Id: afb62a3b-c193-4cda-be72-23c0af7d600f
X-Runtime: 0.208985
Server WEBrick/1.3.1 (Ruby/2.0.0/2014-05-08) is not blacklisted
Server: WEBrick/1.3.1 (Ruby/2.0.0/2014-05-08)
Date: Wed, 29 Jul 2015 15:14:29 GMT
Connection: Keep-Alive

我希望有人可以帮我解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:0)

更新:请尝试以下方法:
http://blog.andrewray.me/how-to-set-up-devise-ajax-authentication-with-rails-4-0/

config/initializers/devise.rb中,将:json添加到可接受的格式:

config.navigational_formats = [:"*/*", "*/*", :html, :json]

我建议您也查看类似Postman的内容来测试JSON请求。