"CSRF validation failed" when try to send POST request in custom module This is my full codes. I want to create a node with service post method. But I cant post. "CSRF validation failed".

I followed this tutorial: https://www.youtube.com/watch?v=Wz2uaRF9RGc

Anyone suggest something.

&lt;?php
/**
* Block Info
*/
function mymodule_block_info(){
$blocks['mymodule_testing'] = array('info' =&gt; t('Mips Testing Block'));
return $blocks;
}

function mymodule_block_view($delta = ''){
$blocks['content'] = '&lt;h1&gt;MIPS Testing Block&lt;/h1&gt;';

/* POST Request */
$post_data_results = mymodule_create_requested_data();

return $blocks;
}

/* Create data with POST Method */
function mymodule_create_requested_data(){
$path = $_SERVER['HTTP_REFERER'] . 'serve/node';

$node_type = 'apply';
$node_title = 'This is title ' . date('c');
$node_body = 'Node Body';
$node_format = 'filtered_html';

$node_data = '{
    "type": "'. $node_type .'",
    "title": "'. $node_title .'",
    "body": {
        "und": {
            "0": {
                "value": "'. $node_body .'",
                "format": "'. $node_format .'"
            }
        }
    }
}';


$node_options = array(
    'headers' =&gt; array(
        'Content-Type' =&gt; 'application/json',
        'Cookie' =&gt; mymodule_application_authentication('test', '123'),
        ),
    'method' =&gt; 'POST',
    'data' =&gt; $node_data
);

return drupal_http_request($path, $node_options);
}

function mymodule_application_authentication($username, $password){

if(isset($_COOKIE['mymodule_authentication_name']) &amp;&amp; isset($_COOKIE['mymodule_authentication_id'])){
    return '' . $_COOKIE['mymodule_authentication_name'] . '=' . $_COOKIE['mymodule_authentication_id'];
} else {
    $login_url = $_SERVER['HTTP_REFERER'] . 'serve/user/login';

    $user_data = '{
        "username" : "'. $username .'",
        "password" : "'. $password .'"
    }';

    $user_options = array(
        'headers' =&gt; array('Content-Type' =&gt; 'application/json'),
        'method' =&gt; 'POST',
        'data' =&gt; $user_data
    );

    $result = drupal_http_request($login_url, $user_options);
    if($result-&gt;code == '200'){
        $user_data = json_decode($result-&gt;data);

        setcookie('mymodule_authentication_name', $user_data-&gt;session_name);
        setcookie('mymodule_authentication_id', $user_data-&gt;sessid);

        return '' . $user_data-&gt;session_name . "=" . $user_data-&gt;sessid .'';
    } else return FALSE;
}

}
You need to get a CSRF token before making your POST call.

function getCsrfToken(callback) {
  jQuery
    .get(Drupal.url('rest/session/token'))
    .done(function (data) {
      var csrfToken = data;
      callback(csrfToken);
    });
}


and use this in your ajax call as a new header:

  jQuery.ajax({
    url: 'http://example.com/entity/node?_format=hal_json',
    method: 'POST',
    headers: {
      'Content-Type': 'application/hal+json',
      'X-CSRF-Token': csrfToken
    },
    data: JSON.stringify(node),
    success: function (node) {
      console.log(node);
    }
  });


https://www.drupal.org/docs/8/core/modules/rest/3-post-for-creating-content-entities