1

I am trying to create nodes with authentication with the services module enabled. So far I have done following things:

  • Get services token from http://localhost/drupal/services/session/token.

  • User authentication: I did trigger login web service and in return I got session name, id and complete user information. I called this web service by passing X-CSRF-Token which I got from previous web service. (http://localhost/drupal/myendpoint/user/login.json`)

  • Node creation: I want to create article content type node. I am passing fields values in JSON and CSRF token and session information as following. (http://localhost/drupal/myrest/node.create)

    Body (application/x-www-form-urlencoded)

    {
       "title":"Created through service",
       "type":"article",
       "body": {
          "value": "<p>test</p>\n",
          "format": "filtered_html"
       }
    }
    

    Headers

    • X-CSRF-Token: bla bla bla

    • `SESSion name: bla bla bla

The response I received from node create web service is the following:

Status Code: 406
Date: Fri, 04 Dec 2015 23:43:31 GMT
X-Content-Type-Options: nosniff
Server: Apache/2.4.9 (Win32) OpenSSL/1.0.1g PHP/5.5.12
X-Powered-By: PHP/5.5.12
Content-Type: text/html
Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0
Connection: Keep-Alive
Keep-Alive: timeout=5, max=100
Content-Length: 0
Expires: Sun, 19 Nov 1978 05:00:00 GMT

Could you please let me know what I am doing incorrect while triggering node create web service?

1

Change the URL to http://localhost/drupal/myrest/node, the application type to application/json, and the request method to POST; then, it should work.

  • Hi, thanks. If I do as per your instructions, it shows me 401 ["CSRF validation failed"] error. – Ravi Maniyar Dec 5 '15 at 12:42
0

After digging much I found solution by my self:

I will post step by step process to create a node with authentication.


Step 1: Get initial token.

    URL: `http://localhost/drupal/services/session/token`

    Method: GET

Step 2: Login

    URL: `http://localhost/drupal/myrest/user/login.json`

    Method: POST

    Content-Type: application/json

    Request payload - request parameter:

    X-CSRF-Token: <Token received from first web service response>

Step 3: Create node.

    URL: `http://localhost/drupal/myrest/node`

    Method: POST

    Content-Type: application/json

    Raw body:

    `{
          "title": "Sample post",
          "type": "article"
     }`

     Custom headers - request parameters:

     <session_name_value_received_from_previous_service_response>: <session_id_value_received_from_previous_service_response>

     Example: 

     SESS6a4470a2012aa23a7f83ea20ed1df766: KS43Muo_odeHA1KPI_kREWP6oK

     X-CSRF-Token: <token_value_received_from_previous_service_response>

     Example:

     X-CSRF-Token: JALApVoib7bGSFFh-59Saj9z5pT8q_5kP9BrHgGIO3E

And I am getting successful response:

{"nid":"4","uri":"http://localhost/drupal/myrest/node/4"}

Thanks all, please continue this thread if you face such issues. I will try to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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