How to set a Basic Auth. for a REST web service?

Hello,

I would like to create a PUT request for a REST web service interface by using the Servoy http plugin.

How can I set the ‘Authorization’ Header by a Basic Authentication RFC2045-MIME (Base64) like:

var _basicAuthentication = "Basic " + window.btoa('XXXX123:SECRET');

var _httpClient = plugins.http.createNewHttpClient();
var _putRequest = _httpClient.createPutRequest(_url);
_putRequest.addHeader('Authorization', _basicAuthentication);

??

Is this

var _basicAuthentication = "Basic " + window.btoa('XXXX123:SECRET');

possible (if yes how) with the Servoy http plugin?

Regards Thomas

a request has a method executeRequest(user,pw)
that should use basic authentication as far as i know

Hi Johann,

I tried the method “executeRequest(user,pw)”, but the Authentication fails because of the incorrect encoded string.

I read some infos about the base64 encoding/decoding strings in JavaScript. In my case I need the “btoa()” function, but I don’t get it work. This function is missing in Servoy…

Edit:
Besides the Authentication should be send by an Header field like “addHeader(‘Authorization’, _basicAuth);”.

Regards Thomas

The http plugin has an addHeader(…) method. And the boa function is just base64 string encoding of which you can find many examples to implement directly.

With that said, the http plugin can be buggy even when you have everything properly setup. Highly recommend using cURL from the command line instead (viewtopic.php?f=22&t=20947&p=112404#p112404). cURL gets updated regularly, 100’s of improvements and bug fixes in the past few years alone.

Hi David,

thank you for your answer.

david:
… the boa function is just base64 string encoding of which you can find many examples to implement directly.

I’m looking for examples to get the btoa function working directly in my method, but without success so far.

Regards Thomas

Hi David,

I wish you a happy new year… and also to all others.

I have found examples for the btoa function and base64 string encoding, but not a way to implement it.
Do you have perhaps a hint for me? This would be very kind.

Regards Thomas

Hi Thomas,

The http plugin’s executeRequest(username, password) approach doesn’t send the authentication string in the header right away. It will send it only when the server responds with a 401 status code and (usually) a WWW-Authenticate header.
Not all REST api’s work that way and require it to be send the first time, i.e. preemptively (like for example with MailChimp).
You can force the basic auth string by adding it yourself in the header like so:

function test() {
    var _sURL = '[your URL]',
        _sAuth = 'Basic ' + encodeBase64('XXX1234:SECRET'),
        _oHttpClient = plugins.http.createNewHttpClient(),
        _oPutRequest = _oHttpClient.createPutRequest(_sURL);

    _oPutRequest.addHeader('Authorization', _sAuth);
    _oPutRequest.executeRequest();
}

/**
 * @param _sInputString
 * @return {java.lang.String}
 */
function encodeBase64(_sInputString) {
    var _oEncoder = Packages.org.apache.commons.codec.binary.Base64,
        _oJString = Packages.java.lang.String;
    return new _oJString(_oEncoder.encodeBase64(new _oJString(_sInputString).getBytes()));
}

Another way would be by using Velocity Services (built into the VelocityReport plugin.
You setup your config.json file like so:

yourMainSolutionName: {
    services: {
        putMethod : {
            url:"$url"
            method: "put",
            processType: "client",
            authentication: "basic",
            preemptive: true,
            userParam: "user",
            passParam: "pass",
            params: {
                url: "String"
            }
        }
    }
}

And then call it like so:

plugins.Velocity.invokeService('putMethod', { url: '[your URL]', user: 'XXXX1234', pass: 'SECRET' });

(You might want to add the params you want to send in the body of course)

Do keep in mind that Basic authentication strings are send in the clear when using HTTP (Base64 is no encryption). So make sure you use the HTTPS protocol so things are send encrypted over the line.

TIP: You can test your code using RequestBin. It will show exactly what your are sending in the headers and body.

Hope this helps.

Hi Robert,

this helps me a lot! Thank you!

I will try to make it by using the Velocity Services.

Regards Thomas