3
votes

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
2
302 is a redirect, have you tried following it? - Dan
The redirect appears to be the typical first response from a digest request, redirecting to the same URL but providing the cnonce and other info. The followAllRedirects option in the first attempt shown above is supposed to handle the redirect, but since that gives a 401, it seems it isn't doing so correctly. - Dave Cassel
I think { sendImmediately: false } in the auth object is supposed to handle the initial 401 in a digest negotiation; I'm not sure what's going wrong here. I've used request with ML7 extensively here: github.com/joemfb/mlpm-registry/blob/master/server.js; note createOrUpdateUser() and saveRenderedMarkdown() (and, yes, that code is a mess ;)). - joemfb

2 Answers

3
votes

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.

0
votes

I am pretty certain that the solution when I encountered this problem was to change the authentication setting in the app server from "digest" to "digestbasic". (Answering because I don't have enough rep to comment anywhere.)