I'm using services 3.5 and I'm getting some troubles with rest server and session authentication.
I'm getting the "CSRF validation failed" error while trying to perform a create action on my resource.
My action should store data in a custom table in database and the create action requires authentication by default.
These are the steps I'm using to test (with curl in a ash script) my back end.
# Login saving cookies
curl -v -i -H "Content-type: application/json" -c cookies.txt -X POST -d '{"username":"myusername","password":"mypassword"}' "$login_url"
# Getting token
token="$(curl "$token_url")"
# Perform create action
curl -v -i -H "X-CSRF-Token: $token" -H "Content-type: application/json" -b cookies.txt -X POST -d '{"name":"myname", "surname":"mysurname", "email":"myemail"}' "$action_url"
I also tried with:
-H "X-CSRFToken: $token"
I found two different name for the CSRF Token header.
Two questions:
Is there a way to solve this problem?
Is there a way to avoid authentication for the action?
Thank you.
Comments
Comment #1
nimbuz77 commentedComment #2
James NBHD commentedI am having a similar problem.
I'm making two requests one to login/autheticate the user and a second to create a taxonomy term.
The User login CSRF is working correctly but I get a validation error on the taxonomy term create curl.
Both the CSRF calls are made using the function.
Comment #3
nimbuz77 commentedIf you mean a data validation I think you pass the point I'm referring to.
I get a correct user authentication with the first call:
curl -v -i -H "Content-type: application/json" -c cookies.txt -X POST -d '{"username":"admin","password":"mypassword"}' http://localhost/[...]/endpoint/user/login
But the second call:
curl -v -i -H "X-CSRF-Token: $token" -H "Content-type: application/json" -b cookies.txt -X POST -d '{"name":"aname", "surname":"asurname", "email":"[email protected]"}' "http://localhost/[...]/endpoint/resource"
gives me the error:
Comment #4
marcingy commentedSupport request are not critical
Comment #5
nimbuz77 commentedSorry for the wrong priority, I mean it is "critical" form my point of view :)
I'm completely stuck on this.
Comment #6
James NBHD commentedNot sure whether this will help - https://drupal.org/node/1334758#comment-7621035. It points out that
.
It worked for me.
Comment #7
nimbuz77 commentedYou mean by attaching the session cookie to the token request?
Comment #8
nimbuz77 commentedYes, I mean that.
It worked for me also
Comment #9
derekwebb1 commentedFor me I created a getToken function in my little ajax helper script:
Then, in the node create side:
This solves the issue for me. I hope it helps someone.
Cheers
Comment #10
kylebrowning commentedComment #11
hmuthukumar commentedThanks @derekwebb1 your solution getting the token and passing as X-CSRF-Token for future calls #9 worked for me
Also to solve the original issue posted in the question you may need to set the cookie for the gettoken curl call.