3

We are trying to call a JSON based API using requestjs, but we're getting an error from the server.

request.get({
            url: 'https://api.myapi.com',
            timeout: 120000,
            headers: {
                username: 'myuser',
                password: 'mypassword'
            }
        }, function (err, result) {
           ...
        }
)

The error:

java.lang.IndexOutOfBoundsException: Index: 0
    java.util.Collections$EmptyList.get(Collections.java:4454)
    com.itemmaster.api.rest.svc.authentication.OpenSSOUserAuthServiceImpl.authenticate(OpenSSOUserAuthServiceImpl.java:159)
    com.itemmaster.api.rest.profiler.SecurityProfiler.profile(SecurityProfiler.java:58)

While running the same call using curl returns without any issue.

curl --header "username:myuser" --header "password:mypassword" 'https://api.myapi.com'
Guy Korland
  • 9,139
  • 14
  • 59
  • 106

1 Answers1

0

If the curl command is working with the syntax you've provided, the following code should work in node.js.

var request = require('request');

var headers = {
    'username': 'myuser',
    'password': 'mypassword'
};

var options = {
    url: 'https://api.myapi.com',
    headers: headers
};

function callback(error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body);
    }
}

request(options, callback);
Parrett Apps
  • 569
  • 1
  • 4
  • 14