1

As explained in the official Sitecore documentation, I'm using the following block of code to login into the Sitecore.

module.exports = function (context, req) {
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function () {
        /*context.log('ReadyState='+this.readyState);
        if(this.readyState === 4 && this.status === 200) {
        context.log('Response='+this.responseText);
        }*/

};

xhr.open("POST", "https://content-XXXXX.com/sitecore/api/ssc/auth/login",true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send('{"domain":"sitecore","username":"XXXXX","password":"XXXXXX"}');

    context.done();
};

The request sends back a token to inform the authentication succeeded, but the documentation does not explain how to use it to send another POST request, for example to creating an item in the content tree. How should I use it ?

xhr2.open("POST", "https://content-XXXXXXXXX/sitecore/api/ssc/item/sitecore%2Fcontent%2Fhome",true);
xhr2.setRequestHeader("Content-Type", "application/json"); <-- TOKEN HERE ?
xhr2.send('{"ItemName": "MyTest","TemplateID":"XXXXXXXXX","Title":"Sitecore","Text":"MyTestText"}');

Thank you in advance for your help

  • have you tried adding "token" header and the value? – josedbaez Jun 21 '18 at 14:19
  • Yes i tried by adding this line : xhr2.setRequestHeader('Authorization', 'Bearer ' + json['token']); where token is the token i got back after login – Slrg Jun 21 '18 at 15:11
  • what about xhr2.setRequestHeader('token', json['token']); – josedbaez Jun 21 '18 at 15:21
  • Aren't you missing api keys ?? See iamlearningsitecore.wordpress.com/2018/06/01/… – Peter Procházka Jun 22 '18 at 6:29
  • @josedbaez. It worked like this, thank you ! – Slrg Jun 22 '18 at 8:22
2

You need to pass the token (received in the first request) in the header like:

xhr2.setRequestHeader('token', val);

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.