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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With