0

I am trying to create a node from a different server via REST API, utilizing the Drupal 8 REST API and the Poster plugin for the browser. I posted the request as shown below, and I got the following output.

{
    "message": ""
}

enter image description here

I already enabled the service end_point to create, retrieve, update, and delete the content in the REST API configuration.

enter image description here

enter image description here

What is happening? Is it the right way of doing it?

3
  • 1
    I don't know the D8 drupal.org/project/services but they state "Services 4.x is currently in development. Since Drupal 8 contains REST functionality in CORE, Services in Drupal 8 will be slightly different. Were aiming to accomplish the following for D8." Guess you should checkout drupal.org/docs/8/core/modules/rest/… Commented Oct 25, 2017 at 14:50
  • 1
    Technically you don't need the Services module in Drupal 8 to create nodes via REST. This page will also be helpful: drupal.org/docs/8/core/modules/rest/… Commented Oct 25, 2017 at 14:56
  • 1
    @tyler.frankenstein . Actually am trying to create a node from another server . Thats is the scenario .
    – KTM
    Commented Oct 25, 2017 at 15:11

3 Answers 3

2

Okay, I am going to Explain you in a very simple way using a Postman Software.
1. Enable the restui Module.
2. Add the basic_auth authentication.
3. Get the X-CSRF-Token from /rest/session/token.
4. Follow things by this image:

enter image description here
5. Add the following text into your Body.


enter image description here

And hit the Send Button Its Done!

2

Drupal officially support 2
Authentication providers

  1. Cookie
  2. Basic_auth

( Note ) Personally, I recommend ‘Cookie’,
because it is useful for mobile and third-party developers.

================= Login ==================
How can Login Drupal’s APIs in PostMan ?
Method: Post
URL : http://localhost/drupal_8_bs4_theme_01/user/login
Parameters: Key=value ---> ?_format=json
Headers : Content-Type = application/json
Body : Raw → application/Json
{ "name": "test", "pass": "test" }

Response :
{ "current_user": { "uid": "25", "name": "test" }, "csrf_token": "-Cgh_1vMeVvFn3E5KePfXKWPeVg2Rhf6kURcnVEQbwA", "logout_token": "PxfO3uR4gUWFn5hWXgvKBr-4gsB8HMOdT4v5okrZxC8" }

======== Create Node Using Cookie Auth ======
After Login How can You Create Node ?
Method: Post
URL : http://localhost/drupal_8_bs4_theme_01/entity/node?_format=json
Headers :
Content-Type = application/json
X-CSRF-Token = 8km8Kh5enGfT1_fpYGg6q6g3BWUhjMa3L3EijUphJu8
Body:
{ "type":[{"target_id":"page"}], "title":[{"value":"Hello World 221"}], "body":[{"value":"How are you?"}] }

======== Create Node Using Basic Auth ======
Method: Post
URL :- http://localhost/drupal_8_bs4_theme_01/entity/node
Query Params :
Key = _format
Value = hal_json
Authorization:
Basic Auth: u -> admin and password -> admin
Headers:
X-CSRF-Token = Token Which you get like CVieBUrFLSjUsXLGLJ-xW6krdKdAcOkpJK0WP6rzK_0
Content-Type = application/hal+json
Accept = application/hal+json

Body:
{ "type":[{"target_id":"page"}], "title":[{"value":"Hello World 221"}], "body":[{"value":"How are you?"}] }

==================== LogOut =======================
How can Logout Drupal’s APIs in PostMan ?
Method: Post
URL: http://localhost/drupal_8_bs4_theme_01/user/logout
Parameters: Key=value ---> ?X-CSRF-Token=-Cgh_1vMeVvFn3E5KePfXKWPeVg2Rhf6kURcnVEQbwA

(Note) I Think Body is Optional

Body : Raw → application/Json
{ "csrf_token": "hU2BZsfvXjcT92rWvxQyiZ1wOD-bMtOkG2Mlj2pSPuw", "logout_token": "UUiitmj854Qm_UpuVZq-PK_0FOXQA9wMIqhVLbXBbFM" }

Response :
Redirect to URL

For Details Visit Following Links
https://www.drupal.org/docs/8/modules/jsonapi/get-post-patch-and-delete
https://www.drupal.org/node/2806623
https://www.drupal.org/docs/8/core/modules/rest/javascript-and-drupal-8-restful-web-services

0

My plan was first make it work using the postman client and port the same using curl request . So first i made a curl request from command line as

curl --include --request POST --user username:password --header 'Content-type: application/hal+json' http://192.168.1.243/node --data-binary '{"_links":
{"type":{"href":"http://192.168.1.243/rest/type/node/article"}}, "title":[{"value":"My first page"}],"body":[{"value":"How are you?"}]}'

And that was worked (created the node in 192.168.1.243) . Then i made the curl request from php as follows :

$data = '{"_links":
{"type":{"href":"http://192.168.1.243/rest/type/node/article"}}, "title":[{"value":"My first page"}],"body":[{"value":"How are you?"}]}';

        $url = "http://192.168.1.243/node";
        $curl_connection = curl_init($url);
        curl_setopt($curl_connection, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");
        curl_setopt($curl_connection, CURLOPT_COOKIEJAR, "/tmp/cookie.txt");
        curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl_connection, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl_connection, CURLOPT_HTTPHEADER, array('Content-type: application/hal+json'));
        curl_setopt($curl_connection, CURLOPT_USERPWD, "$username:$password");
        curl_setopt($curl_connection, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

And that was all . The node created in the remote server .

In postman client cant set the HEADER as 'Content-type: application/hal+json' . I think that may caused the issue . Any insights welcome .

Your Answer

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

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