i want to create an article by rest services of drupal ? as given in the documentation on Drupal Post docs link
but it gives an error Reference error : Drupal is not defined
in the following line of code jQuery.get(Drupal.url('rest/session/token'))
here is my code based on docs
function getCsrfToken(callback) {
jQuery.get(Drupal.url('rest/session/token'))
.done(function (data) {
var csrfToken = data;
callback(csrfToken);
});
}
function postNode(csrfToken, node) {
jQuery.ajax({
url: 'http://localhost/dr/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);
}
});
}
var newNode = {
_links: {
type: {
href: 'http://localhost/dr/rest/type/node/article'
}
},
type: {
target_id: 'article'
},
title: {
value: 'Example node title'
}
};
getCsrfToken(function (csrfToken) {
postNode(csrfToken, newNode);
});