HTTP basic access authentication

How to authenticate using plugins.http.createNewHttpClient()?

What is the purpose of the parameters defined in executeRequest(username, password)?
The documentation says nothing.

The following code dose not work:

    var httpClient = plugins.http.createNewHttpClient();
    var url = 'http://localhost:8080/service'; // requires authentication
    var getRequest = httpClient.createGetRequest(url);
    var username = 'foo';
    var password = 'bar';
    var response = getRequest.executeRequest(username, password);
    application.output(response.getStatusCode());

Result is:
403.0

The following code works as I expect:

    var httpClient = plugins.http.createNewHttpClient();
    var url = 'http://localhost:8080/service'; //requires authentication
    var getRequest = httpClient.createGetRequest(url);
    var username = 'foo';
    var password = 'bar';
    getRequest.addHeader('Authorization', 'Basic ' + Base64.encode(username + ':' + password));
    var response = getRequest.executeRequest();
    application.output(response.getStatusCode());

result is:
200.0

vasil,

I tried the same (first sample) code and it worked as expected: 200

Note that http 403 is not Unauthorized (which is 401) but Forbidden, so I guess the user has logged in but is not allowed.

Rob

Rob, you are right. It works.

I have to provide more information.
I am trying to interact with RESTful API. The server requires preemptive authentication.
I think that plugins.http does not provide out of the box preemptive authentication.

Vasil,

If you file a feature request in our support system we will have a look if that is feasible for the plugin.

Rob