3

I am using a 3rd party REST API in my node server. The 3rd party API provider as given me an API key and an example in cURL as follows:

$ curl -u APIKey@https://xyzsite.com/api/v1/users

I am not sure how I do it in node js. I have tried following but no luck. I get

var options = {
  host: "xyzsite.com",
  path: "/api/v1/users",
  headers: {
    "Authorization": "Basic " + myAPIKey
  }
};


https.get(options, function(res, error) {
  var body = "";
  res.on('data', function(data) {
    body += data;
  });
  res.on('end', function() {

    console.log(body);
  });
  res.on('error', function(e) {
    console.log(e.message);
  });
});

Console message

{
  "message": "No authentication credentials provided."
}
1
  • What API are you using? You're either not supplying the correct credentials or your request contains errors. Commented Mar 18, 2016 at 16:31

1 Answer 1

1

Change your request like this..

https.get(myAPIKey + "@https://xyzsite.com/api/v1/users",function(res,error){
  var body = "";
  res.on('data', function(data) {
    body += data;
  });
  res.on('end', function() {

    console.log(body);
  });
  res.on('error', function(e) {
    console.log(e.message);
  });
});

Or, if you want to use the options object..

var options={
  host:"xyzsite.com",
  path:"/api/v1/users",
  auth: myAPIKey
};


https.get(options,function(res,error){
  var body = "";
  res.on('data', function(data) {
    body += data;
  });
  res.on('end', function() {

    console.log(body);
  });
  res.on('error', function(e) {
    console.log(e.message);
  });
});

More info on NodeJs https options

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply. But unfortunately, it did not work. The first approach threw Error: Unable to determine the domain name. And the second one the same error, No authentication credentials provided.
Yeah, I guess I'd have to know more about the API to determine why it's not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.