简体   繁体   中英

401 with node.js request

I have an url that when simply used on a browser gets results, but when used via request throws 401.

var request = require('request');
request('http://server/platform/views/test.aspx?type=broadband&userdata=parameters', function (error, response, body){
if (!error && response.statusCode == 200) 
    console.log(body);          
else 
    console.log(response.statusCode);
})

I replaced my url with google.com and I do get results, so is there anything I need to tell request or something to be handled on the server I want to get data from?

401 in HTTP means that you are trying to access a ressource without any autorisation (you are not authenticated).

If the browser can access it, it's maybe because it has a stored cookie for that. Or maybe the server is parsing the User-Agent of the browser.

When you do a request with request like this, you have no cookie and minimal headers in your request (no User-Agent, no Referrer, ...).

I report documentation link: https://www.npmjs.com/package/request#http-authentication

also there is a tricky part here, sendImmediately should be set to false, because some server still send 401 reply if authentication headers are sent immediately

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM