1

I have one problem with HTTP GET/POST request. When I use the DHC/Postman, send the parameters to the URL + endpoint, works perfectly. 200 is returned.

But with code, like my example, show one 401 error.

I have searched about that and the problem is with the auth, not sure, see... Maybe is the same.

With this explanation, need to set the Authorization, I think. But the problem is when I access the site, the auth is automatic, see:

enter image description here

My code:

var jsonObject = JSON.stringify({ "UserName": login});

// prepare the header
var postheaders = {
    'Content-Type' : 'application/json',
    'Content-Length' : Buffer.byteLength(jsonObject, 'utf8')
};

// the post options
var optionspost = {
        host: "xxxxxxxxxx.com",
  //      path: '/Home/endpoint', //send the data for the endpoit with Postma works fine
        method: 'POST',
        headers : postheaders
};

console.info('Options prepared:');
console.info(optionspost);
console.info('Do the POST call');

// do the POST call
var reqPost = http.request(optionspost, function(res) {
    console.log("statusCode: ", res.statusCode);
    // uncomment it for header details
//  console.log("headers: ", res.headers);

    res.on('data', function(d) {
        console.info('POST result:\n');
        process.stdout.write(d);
        console.info('\n\nPOST completed');
    });
});

// write the json data
reqPost.write(jsonObject);
reqPost.end();
reqPost.on('error', function(e) {
    console.error(e);
});

Obs.: This website it's from my Company (.NET) and is Integrated with IIS (Active Directory login users for authenticate), when I access, automatically is logged... I really don't know how to solve this.

Obs II.: I Try to use one anonymous new tab and use DHC online, and my post doesn't work. This application just works inside network company and with Client side (Using postman with my computer).

Obs III.: The request is from Server and the login from my server have all permissions to access this site, and when I request, is like I'm anonymous, but if I did the same with REST Client/Postman, works perfectly. I need that it works with http request from my Server.

1
  • Were you able to find a fix for this? Commented Feb 22, 2018 at 14:20

2 Answers 2

2

You can use a module like ntlm-webapi which will allow you to use NTLM auth. That way the request will go through. Just make sure the user you use is authorized for that server.

var Request = require('ntlm-webapi');

var request = new Request({
    url: "http://some.restful.api.org/you/want/to/call",
    username: 'username',
    password: 'password',
    domain: 'company_domain'
});

request.get(function(err, result){

    if (err) console.log (err);

    console.log (result);
});
Sign up to request clarification or add additional context in comments.

Comments

1

It seems that you forgot to add the Authorization header in your code

// prepare the header
var postheaders = {
    'Authorization' : 'Negotiate '+ yourAccessKey,
    'Content-Type' : 'application/json',
    'Content-Length' : Buffer.byteLength(jsonObject, 'utf8')
};

1 Comment

This authorization is integrated with IIS and changed every time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.