您如何使用 cURL 在使用 Devise 的 Rails 应用程序上进行身份验证? [英] How do you use cURL to authenticate on a Rails application that uses Devise?

查看:27
本文介绍了您如何使用 cURL 在使用 Devise 的 Rails 应用程序上进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 cURL 测试我的 rails web 服务,但到目前为止我只能在停用 before_filter :authenticate_user! 的情况下进行.但我想先验登录,然后创建、销毁等.

我看到使用 cURL 可以验证会话信息并将其保存在 cookie 中,用于以下请求,但我无法使用 Devise:在我的场景中,用户使用电子邮件登录/password 组合,所以我正在尝试:

<前>卷曲-X POST -d 'email=email@example.com&password=password' -c cookie http://localhost:3000/users/sign_in > out

我得到:ActionController::InvalidAuthenticityToken in Devise/sessionsController#create

有人能给我一个想法/一个例子吗?

谢谢!

解决方案

这有效:

验证:

curl -H 'Content-Type: application/json' -H '接受:应用程序/json' -X POST http://localhost:3000/users/sign_in -d "{'user' : { 'email' : 'test@example.com', 'password' : 'password'}}" -c cookie

显示:

curl -H 'Content-Type: application/json' -H '接受:应用程序/json' -X GET http://localhost:3000/pages/1.xml -b cookie

I want to test my rails web service using cURL, yet so far I could only do it while deactivating before_filter :authenticate_user!. But I'd like to log in a priori and then create, destroy etc.

I saw that with cURL you can authenticate and save the session info in a cookie, that you use for your following requests, yet I couldn't make it work with Devise: in my scenario, the user logs in using an email/password combination, so I'm trying:

curl 
  -X POST 
  -d 'email=email@example.com&password=password' 
  -c cookie 
  http://localhost:3000/users/sign_in > out

and I get: ActionController::InvalidAuthenticityToken in Devise/sessionsController#create

Could somebody give me an idea / an example?

Thanks!

解决方案

This works:

Authenticate:

curl -H 'Content-Type: application/json' 
  -H 'Accept: application/json' 
  -X POST http://localhost:3000/users/sign_in 
  -d "{'user' : { 'email' : 'test@example.com', 'password' : 'password'}}" 
  -c cookie

Show:

curl -H 'Content-Type: application/json' 
  -H 'Accept: application/json' 
  -X GET http://localhost:3000/pages/1.xml 
  -b cookie

这篇关于您如何使用 cURL 在使用 Devise 的 Rails 应用程序上进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆