aacerox / node-rest-client

REST API client from node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use with auth and json options

Jakobud opened this issue · comments

I have a particular REST api I can connect to with the following code:

const options = {
    body: 'grant_type=client_credentials',
    auth: id + ':' + password,
    json: true,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  }
https.request(endpointUrl, options, function(res) {

  // do something with res

})

How do I make this same connection using node-rest-client?

I tried this and a few variations but didn't get anywhere. Server is telling me invalid client so the formatting is definitely not right.

let args = {
  data: {
    grant_type: 'client_credentials'
  },
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  // user: id,
  // password: pass,    // also tried user and pass separate but no dice. 
  auth: id + ':' + pass,
  json: true
}

client.post(accessTokenEndpoint, args, (data, res) => {
  console.log(data)
})

any clues?

I figured it out. I was not passing id and pass into the client. That fixed it. Thanks