Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I POST a message with digest authentication using Node?

I'm trying to POST a message to a MarkLogic application server with digest security using Node. The equivalent curl request works fine:

curl -v -X POST --anyauth -u admin:admin --header "Content-Type:application/json" \
  -d '{"user-name":"joe", "password": "cool"}' http://localhost:8002/manage/v2/users

I tried using the NPM request module, which says it supports digest requests. I was able to do a GET request successfully. Here's one attempt at POST:

request(
  {
    'url': 'http://localhost:8000/manage/v2/users',
    'method': 'POST',
    'auth': {
      'user': 'admin',
      'password': 'admin',
      'sendImmediately': false
    },
    'followRedirect': true,
    'followAllRedirects': true,
    'json': true,
    'body': {'user-name':'joe', 'password': 'cool'}
  },
  function(error, response, body) {
    console.log('callback: ' + response.statusCode);
  }
);

That get's me a 401. The username and password are correct. I also tried like this:

request
  .post(
    'http://localhost:8000/manage/v2/users', 
    {'user-name':'joe', 'password': 'cool'})
  .auth('admin', 'admin', false)
  .on('response', function(response) {
    console.log('response: ' + JSON.stringify(response));
  })
  .on('error', function(error) {
    console.log('error: ' + error);
  });

That gets me a 302. I feel like I must be missing something straightforward here.

  • node v0.10.31
  • MarkLogic 8.0-2
like image 367
Dave Cassel Avatar asked Mar 16 '26 13:03

Dave Cassel


1 Answers

This turned out to have a simple solution: the curl request worked because I used localhost:8002; the node request failed because I used localhost:8000. Duh. The 302 redirect was telling me to use 8002 and I just missed that.

like image 102
Dave Cassel Avatar answered Mar 18 '26 02:03

Dave Cassel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!