Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use cURL to authenticate on a Rails application that uses Devise?

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 protected]&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!

like image 326
Marius Butuc Avatar asked Jan 12 '11 19:01

Marius Butuc


1 Answers

This works:

Authenticate:

curl -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -X POST http://localhost:3000/users/sign_in \
  -d "{'user' : { 'email' : '[email protected]', '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
like image 57
Marius Butuc Avatar answered Nov 01 '22 16:11

Marius Butuc