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

6
  • have you tried adding "token" header and the value?
    – josedbaez
    Commented Jun 21, 2018 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
    Commented Jun 21, 2018 at 15:11
  • what about xhr2.setRequestHeader('token', json['token']);
    – josedbaez
    Commented Jun 21, 2018 at 15:21
  • Aren't you missing api keys ?? See iamlearningsitecore.wordpress.com/2018/06/01/… Commented Jun 22, 2018 at 6:29
  • @josedbaez. It worked like this, thank you !
    – Slrg
    Commented Jun 22, 2018 at 8:22

1 Answer 1

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 and acknowledge you have read our privacy policy.

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