Drupal - How do I POST a new node?
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
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
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
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
By using Session name and ID in the POST HEADER, we can able to create new nodes
Now after adding the HEADER, You need to make HTTP POST request and make sure user should have permissions to create node.
If the node is created you will get the response with node link
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
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
Step 2: Issue a post request to node
with 2 headers
- Cookie: [session_name]=[sessid]
- X-CSRF-Token: [token]
Screenshot from google chrome postman