16

I have referred many links including the following:

I tried several ways to POST a new node to a Drupal site, but I have failed. I have created a sample CCK for storing students data with name, age, gender, DOB, and added some list.

I have installed and configured the Services module (D7), and created a service end point at services.
As expected, GET is the easiest way to get details of a node in JSON. http://localhost/drupal/services/node simply listed all nodes.

I still can't figure out how to use the POST method.

  1. I would like to search for the students whose name contains john. I tried localhost/drupal/services/node&node[name]=john. I used POST Man plugin for Chrome without success. I also tried some other ways.

  2. I tried adding a new student to the site without success.

Can anyone guide me to make the above work fine?

UPDATE :

I tried adding new nodes of type article . It worked fine. but when i am trying to add nodes of type Department CCK It is showing "s:22:"CSRF validation failed";" Here are the screen shots

I have gone through the link https://drupal.org/node/2013781

I followed session = session_name=session_id but i still get an error.
enter image description here enter image description here enter image description here

1
  • What happens when you try to POST a node? Do you get an error message? If so, what is it?
    – Gaelan
    Jun 28, 2013 at 6:28

3 Answers 3

22
+50

Hope this will help you,

For Drupal 7

Tools used:

  • Firefox Poster add-on to do tests and debugging. There may be other add-on as well. But for this example im using Firefox add-on.
  • To help editing and validating the JSON data you can use
    • Edit the JSON data using this link
    • Validate the JSON data using this link

HTTP requests

  • To make the HTTP requests, you need use the Firefox add-on POSTER.

  • Two commonly used methods for a request-response between a client and server are:

    • GET
    • POST
  • After the add-on is Downloaded. Open Firefox -> Tools -> Poster

    enter image description here

  • Make sure to enable the Session authentication. After the Services is configured properly, before creating the node you should log-in. Once logged-in you can create the nodes.

  • For login use POST method, with the URL like - http://XXXXXXXX.com/demo/rulebook/rulebook_endpoint/user/login

    enter image description here

  • If it is a valid user then it will return user details with session ID and Name. If not valid it will return Access denied

    enter image description here

  • By using Session name and ID in the POST HEADER, we can able to create new nodes

    enter image description here

  • Now after adding the HEADER, You need to make HTTP POST request and make sure user should have permissions to create node.

    enter image description here

  • If the node is created you will get the response with node link

    enter image description here

NOTE : If you want the response in JSON then, you should always append .json at the end of the URL. eg., http://XXXXXXXX.com/test/endpoint/user.json OR http://XXXXXXXX.com/test/endpoint/node.json OR http://XXXXXXXX.com/test/endpoint/user/user_id.json OR http://XXXXXXXX.com/test/endpoint/node/node_id.json

7
  • Sorry i unchecked the form url-encoded option in services module. and here is the response 401: Unauthorised : CSRF authorisation failed. what might be causing this? Many thanks
    – Ramaraju.d
    Jul 1, 2013 at 1:55
  • hi @inizio may i know on what basis did you assigned session value in HEADERS. Can you make it clear. Example, the sessionId i got is SESS29af1facda0a866a687d5055f2fade2c but in your answer it has more length !
    – Ramaraju.d
    Jul 1, 2013 at 4:22
  • Session value in HEADERS consist of session_name and session_id. ie., session is the variable and its value consist of session_name = session_id. session = (session_name = session_value)
    – Inizio
    Jul 1, 2013 at 5:58
  • It says 401: Unauthorized : CSRF validation failed. I gave the following as headers name = session value =SESS29af1facda0a866a687d5055f2fade2c=gBVoqtkN2ZNjDyOjIfpz47oQpnmCKytcgHOCHZhKRzo
    – Ramaraju.d
    Jul 1, 2013 at 6:05
  • Yes it worked. But the only issue, The Article CCK has title,field_names, body but only title is being saved and node is created, but body and names field are empty. How to overcome this.
    – Ramaraju.d
    Jul 1, 2013 at 6:31
8

For Drupal 7 and Services 7.x-3.x

Step 1: Issue a post request to user.login resource with your username and password.

If the login is successful, you will get a response object with 4 properties:

  • session_name
  • sessid
  • token
  • user (object)

Screenshot from google chrome postman Screenshot from google chrome postman

Step 2: Issue a post request to node with 2 headers

  • Cookie: [session_name]=[sessid]
  • X-CSRF-Token: [token]

Screenshot from google chrome postman Screenshot from google chrome postman

1
0

If you do POST to the REST API then you will have to put everything into POST data, not to URL. In Postman you can set the request to post, than type to x-www-form-urlencoded and below that you can add your data values. Don't forget to add status, uid, language, type, title and then the extra values. The resource URL should be only HOST/REST_API_PATH/node.

1
  • 1
    Little confusing. Ok can you tell me what should be the parameters for creating a new article.? If you don't mind can you post an example or a link so that i can have a clear concept !
    – Ramaraju.d
    Jun 26, 2013 at 13:03

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.