0

I don't know how to send the curl -u in node js i try the following code but no luck.

request({
            url: "https://preview.twilio.com/HostedNumbers/HostedNumberOrders",
            method: "POST",
            json: true,
            headers: {
                "content-type": "application/json",
                "Authorization": accountSid + authToken
            },
            json: {
                "PhoneNumber": payload.phonenumber, "SmsCapability": true, "IsoCountry": payload.isocountry,
                "AddressSid": address.sid, "Email": payload.email,
                "FriendlyName": payload.friendlyname, "StatusCallbackUrl": "http://example.com/callback",
                "StatusCallbackMethod": "POST",
            },
        }, function (error, response, body) {
            res.json({ 'error': error, 'data': response, 'body': body });
            return true;
        })

this is the curl which i want to call.

curl -XPOST https://preview.twilio.com/HostedNumbers/HostedNumberOrders \ 
-d "PhoneNumber=+18312011484" \
-d "SmsCapability=true" \
-d "IsoCountry=US" \
-d "AddressSid=ADXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
-d "Email=hostedsms@twilio.com" \
-d "FriendlyName=HostedNumberOrder1" \
-d "StatusCallbackUrl=http://example.com/callback" \
-d "StatusCallbackMethod=POST" \
-u 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:your_auth_token'
btzr
  • 2,077
  • 18
  • 25
Yaseen Ahmad
  • 1,807
  • 5
  • 25
  • 43

1 Answers1

2

Twilio api

Twilio supports HTTP Basic and Digest Authentication. This allows you to password protect your TwiML URLs on your web server so that only you and Twilio can access them.

See: docs

Request: http authentication

Digest authentication is supported, but it only works with sendImmediately set to false; otherwise request will send basic authentication on the initial request, which will probably cause the request to fail.

See: docs

var options = {
  url: 'https://preview.twilio.com/HostedNumbers/HostedNumberOrders',
  auth: {
    user: username,
    password: password,
    sendImmediately: true
  }
}

request(options, function (err, res, body) {
  if (err) {
    console.dir(err)
    return
  }
  console.dir('headers', res.headers)
  console.dir('status code', res.statusCode)
  console.dir(body)
})

Note that you can also specify basic authentication using the URL itself, as detailed in RFC 1738. Simply pass the user:password before the host with an @ sign:

var username = 'username',
    password = 'password',
    url = 'http://' + username + ':' + password + '@some.server.com';
 
request({url: url}, function (error, response, body) {
   // Do more stuff with 'body' here 
});
Community
  • 1
  • 1
btzr
  • 2,077
  • 18
  • 25