1

I have a local Drupal instance with services. I created an endpoint that has user resource included. I verified that all is well by logging in using the endpoint. I am now stuck at registering new users through the api. I tried the following:

URL: http://localhost/recipes/api/user/register

Method: POST

Content-Type: application/json

Body: { "name":"user34", "pass":"3323", "mail":"[email protected]", "status":"1" }

Result: 22:CSRF validation failed

I know that this error pops out when the token is not passed in the header of the request but how am I supposed to get the token if I am an anonymous user wanting to register?

Kindly mention the steps that I need to follow to have a user register through the REST api

Thanks :)

1 Answer 1

0

Anonymous (and authenticated) users can both get the token from the same place. Try following these steps:

  1. Get the token with a GET to ?q=services/session/token
  2. Attach the token to your POST header under the name X-CSRF-Token
  3. Make the POST call to register the user
2
  • I tried making a get request to localhost/recipes/api/session/token and using postman, the response had no content in the body. However, if I look at the cookies in the response I can see a key/value pair of a random string. That said, should I use the string in the value of the cookie as the value of the X-CSRF-Token in my next request? Thanks Jun 15, 2018 at 15:23
  • 1
    The services/session/token path is reserved, and does not include your api endpoint path. So don't use api/session/token for the token, instead always use services/session/token. Jun 15, 2018 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.