Example using http-request node with Basic authorization

I have googled and read for hours but have yet to find an example that works. I am invoking the Solcast API, which requires Basic auth and only a username containing an API key but no password.

The issue is I need to pass the username via msg.headers. When I include the username in the node properties dialog, the API call works fine, but I cannot seem to get it right passing it in msg.headers. The last code I tried, which didn't work, was this:

msg.headers = {};
APIkey = flow.get('APIkey');
var auth = 'Basic ' + new Buffer(APIkey).toString('base64');
//var auth = 'Basic ' + APIkey;

msg.ResourceID = flow.get('ResourceID');
msg.headers = {
    "Authorization": auth
}
return msg;

Any help is greatly appreciated.

dave

1 Like

Normally the format is Basic user:password (with the user:password part base64 encoded)

See here

1 Like

Steve, that was very close to what I was using. Since the API doesn't need a password, I was not including the ":". I just tried including the colon and now it works. I could've sworn I tried that before, but I guess not. Thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.