javascript - 401 unauthorized except through browser -
i've been trying html request, using url works in browser somehow results in 401 unauthorized error when run code. problem did provide authentication; url of form
http://username:password@url?param=value
it succeeds in firefox , chrome (i went through incognito mode avoid cookies), google's postman app, despite trying several other http request methods return unauthorized error. i've run through rest api , xmlhttprequest, command line.
any ideas on causing this? or, better, if someone's had similar problem , has solution?
(note: i'm pretty new whole thing, not sure if clear/detailed enough. i'll best elaborate if needs.)
edit: here's idea of original code running:
var func = function() { var options = { baseurl: server, uri: '/device.cgi', qs: { param1: value1, param2: value2 } }; request(options, function(err, response, body) { if (err) console.log(err); else console.log(body); }); };
i ran
function httpget(theurl) { var xmlhttp = new xmlhttprequest(); xmlhttp.open( "get", theurl, false ); // false synchronous request xmlhttp.send( null ); return xmlhttp.responsetext; }
from this question , got same unauthorized result.
and because apparently device i'm using fine post well,
var func = function () { var formdata = { form: { param1: value1, param2: value2 } }; request.post(server + 'device.cgi', formdata, function (err, response, body) { if (err) { console.log(err); } else console.log(body); }).auth(userid, userpass); };
still same result.
the browser taking url, , creates basic http authorization header you.
node.js not you, , must set authorization header in code.
var http = require('http'); var options = { host: 'www.tempuri.org', path: '/helloworld/', headers: { 'authorization': 'basic qwxhzgrpbjppcgvuu2vzyw1l' } }; http.request(options, function(response) { var body = ''; response.on('data',function(c) { body+= c; }) response.on('end',function() { console.log(body) }) }).end();
the following links discuss basic auth, how browser encodes data in url, , how implement in node.js:
Comments
Post a Comment