기본 인증이있는 경우 Node.js에서 http.client를 사용하는 방법
제목에 따라 어떻게해야합니까?
내 코드는 다음과 같습니다.
var http = require('http');
// to access this url I need to put basic auth.
var client = http.createClient(80, 'www.example.com');
var request = client.request('GET', '/', {
'host': 'www.example.com'
});
request.end();
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
Authorization
헤더에 필드 를 설정해야 합니다.
Basic
이 경우 인증 유형 과 username:password
Base64로 인코딩되는 조합이 포함됩니다.
var username = 'Test';
var password = '123';
var auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
// new Buffer() is deprecated from v6
// auth is: 'Basic VGVzdDoxMjM='
var header = {'Host': 'www.example.com', 'Authorization': auth};
var request = client.request('GET', '/', header);
에서 Node.js를 http.request API 문서 과 유사한 무언가를 사용할 수 있습니다
var http = require('http');
var request = http.request({'hostname': 'www.example.com',
'auth': 'user:password'
},
function (response) {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
request.end();
더 쉬운 해결책은 URL에서 직접 user : pass @ host 형식을 사용하는 것입니다.
은 Using 요청 라이브러리 :
var request = require('request'),
username = "john",
password = "1234",
url = "http://" + username + ":" + password + "@www.example.com";
request(
{
url : url
},
function (error, response, body) {
// Do more stuff with 'body' here
}
);
나는 이것에 대해서도 약간의 블로그 포스트를 썼다 .
var username = "Ali";
var password = "123";
var auth = "Basic " + new Buffer(username + ":" + password).toString("base64");
var request = require('request');
var url = "http://localhost:5647/contact/session/";
request.get( {
url : url,
headers : {
"Authorization" : auth
}
}, function(error, response, body) {
console.log('body : ', body);
} );
OSX에서 node.js 0.6.7을 사용하고 있으며 프록시와 함께 작동하기 위해 'Authorization': auth를 얻을 수 없었기 때문에 'Proxy-Authorization': auth로 설정해야했습니다. 내 테스트 코드는 다음과 같습니다. :
var http = require("http");
var auth = 'Basic ' + new Buffer("username:password").toString('base64');
var options = {
host: 'proxyserver',
port: 80,
method:"GET",
path: 'http://www.google.com',
headers:{
"Proxy-Authorization": auth,
Host: "www.google.com"
}
};
http.get(options, function(res) {
console.log(res);
res.pipe(process.stdout);
});
var http = require("http");
var url = "http://api.example.com/api/v1/?param1=1¶m2=2";
var options = {
host: "http://api.example.com",
port: 80,
method: "GET",
path: url,//I don't know for some reason i have to use full url as a path
auth: username + ':' + password
};
http.get(options, function(rs) {
var result = "";
rs.on('data', function(data) {
result += data;
});
rs.on('end', function() {
console.log(result);
});
});
I came across this recently. Which among Proxy-Authorization and Authorization headers to set depends on the server the client is talking to. If it is a Webserver, you need to set Authorization and if it a proxy, you have to set the Proxy-Authorization header
This code works in my case, after a lot of research. You will require to install the request npm package.
var url = "http://api.example.com/api/v1/?param1=1¶m2=2";
var auth = "Basic " + new Buffer(username + ":" + password).toString("base64");
exports.checkApi = function (req, res) {
// do the GET request
request.get({
url: url,
headers: {
"Authorization": auth
}
}, function (error, response, body) {
if(error)
{ console.error("Error while communication with api and ERROR is : " + error);
res.send(error);
}
console.log('body : ', body);
res.send(body);
});
}
'program tip' 카테고리의 다른 글
Pandas는 데이터 프레임을 튜플 배열로 변환합니다. (0) | 2020.08.14 |
---|---|
iframe에서 스크롤바 제거 (0) | 2020.08.14 |
HTML 래퍼없이 DOMDocument의 HTML을 저장하는 방법은 무엇입니까? (0) | 2020.08.14 |
플러그인 설치를 위해 내 FTP 자격 증명을 요구하는 WordPress (0) | 2020.08.14 |
자바에서 모든 공백을 제거하는 방법 (0) | 2020.08.14 |