0

I have following CURL command

curl -u YOUR_API_KEY:x \
 -H 'Content-Type: application/json' \
 -X POST \
 -d '{"first_name":"Tony", "kind":"person", "contact_name":"Stark"}' \
 'https://ACCOUNT_NAME.quadernoapp.com/api/contacts.json'

I want do this request in NodeJS using the request module. This is the code I have written.

var options = {
    uri: 'https://ACCOUNT_NAME.quadernoapp.com/api/contacts.json',
    json: true,
    auth: {
      user: 'YOUR_API_KEY'
    }
    data: {"first_name" : "Tonsdfasdy", "kind":"peasdfarson", "contact_name":"Staasdfadfadrk"}
  }

request.post(options, function cb(){})

But it is not authenticatd properly. What is the error here?

chamathabeysinghe
  • 858
  • 2
  • 13
  • 34

1 Answers1

0

You're authenticating using HTTP Basic authentication in your cURL command, where username and password are provided with the -u option and separated by :, so you need to provide your code with the password, like so :

var options = {
  uri: 'https://ACCOUNT_NAME.quadernoapp.com/api/contacts.json',
  json: true,
  auth: {
    user: 'YOUR_API_KEY',
    password: 'x'
  },
  body: {
    first_name : "Tonsdfasdy", kind:"peasdfarson", contact_name:"Staasdfadfadrk"
  }
}

request.post(options, function cb(){})

And please try to pass your JSON object in an attribute named body rather than data (it will be transformed to a JSON string thanks to the json: trueoption).

You may also want to check this one : how to do Auth in node.js client

Hope this helps!

Philippe Sultan
  • 2,111
  • 17
  • 23
  • Have you tried to get logs from within your callback function : `function(err, res, body) { if (err) { return console.log(err); } console.log(body); }` Also, can you please try to send your JSON in an attribute named `body` rather than `data` ? E.g. : `body: {"first_name" : "Tonsdfasdy", "kind":"peasdfarson", "contact_name":"Staasdfadfadrk"}` – Philippe Sultan Jun 25 '18 at 11:10