Search code examples
javascriptrestdojobasic-authentication

Dojo JsonRest Basic Authentication


I want to read JSON Data from a REST-Service with the Dojo JsonRest. The REST-Service requires Username and Password as Basis Authentication String. For the beginning, I hardcoded this string. Now, I tried the following:

    var processStore = new JsonRest({
    target: "http://host/activiti-rest/service/repository/process-definitions?startableByUser=gonzo", 
    allowNoTrailingSlash: false,
    user: "test",
    password: "test"
    });

But this did not work. Therefore my question: How can I send basic authentication credentials with Dojo JsonRest?


Solution

  • You may try one of two things:

    1) Put the username and password in the target url itself like so:

    var processStore = new JsonRest({
        target: "http://username:password@host/activiti-rest/service/repository/process-definitions?startableByUser=gonzo", 
        allowNoTrailingSlash: false
    });
    

    If the username is an email address, then:

    var processStore = new JsonRest({
        target: "http://useremail%40whatever.com:password@host/activiti-rest/service/repository/process-definitions?startableByUser=gonzo", 
        allowNoTrailingSlash: false
    });
    

    2) Use the header propery of JsonRest:

    var encodedLogin = "Basic " + window.btoa("username:password");
    var processStore = new JsonRest({
        target: "http://host/activiti-rest/service/repository/process-definitions?startableByUser=gonzo", 
        allowNoTrailingSlash: false,
        headers: {
            Authorization: encodedLogin
        }
    });
    

    This solution use base64 encoding on the username and password combination string and then sends it with the "Authorization" header parameter.