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);”.
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.
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.
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:
(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.