0

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.

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

function mymodule_block_view($delta = ''){
$blocks['content'] = '<h1>MIPS Testing Block</h1>';

/* 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' => array(
        'Content-Type' => 'application/json',
        'Cookie' => mymodule_application_authentication('test', '123'),
        ),
    'method' => 'POST',
    'data' => $node_data
);

return drupal_http_request($path, $node_options);
}

function mymodule_application_authentication($username, $password){

if(isset($_COOKIE['mymodule_authentication_name']) && 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' => array('Content-Type' => 'application/json'),
        'method' => 'POST',
        'data' => $user_data
    );

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

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

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

}
  • you might check to see if a POST requires a token (like a form token). that would be checked before it gets to your code and explains the CSRF error, since that's what it's meant to prevent. – troseman Apr 18 '17 at 4:29
  • @avibodha have you any docs/tuts for collect token? It will be much help for me. – Tazim Apr 18 '17 at 5:49
1

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

| improve this answer | |
  • I m using drupal 7. And have you any suggestion for my custom module using php? – Tazim Apr 19 '17 at 17:53
  • yes, suggestion is get a csrf token and put the X-CSRF-Token header in your call to drupal_http_request. – troseman Apr 19 '17 at 21:05
  • ;) hahaha... nice suggestion. thanx. – Tazim Apr 20 '17 at 7:11

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.