本文整理汇总了TypeScript中https.request函数的典型用法代码示例。如果您正苦于以下问题:TypeScript request函数的具体用法?TypeScript request怎么用?TypeScript request使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了request函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: run_container
run_container(image: string, cmd: Array<string>, cb: (exit_status: number)=>void): void
{
var sock_addr_arr = this._host.split(":");
var options: https.RequestOptions =
{
hostname: sock_addr_arr[0],
port: parseInt(sock_addr_arr[1]),
path: "/images/create?fromImage=" + image,
method: "POST",
headers:
{
"Content-Type": "application/json"
},
key: fs.readFileSync(this._cert_path + "/key.pem"), // TODO: make these async
cert: fs.readFileSync(this._cert_path + "/cert.pem"),
ca: fs.readFileSync(this._cert_path + "/ca.pem"),
agent: false
};
console.log("Pulling image...");
var req: http.ClientRequest = https.request(options, function (res: http.ClientResponse)
{
res.on("data", function(data: Buffer) {});
res.on("error", function(e: Error)
{
console.error(e);
cb(-1);
});
res.on("end", function()
{
if (res.statusCode < 200 || res.statusCode >= 300)
{
console.error("Received invalid status code (" + res.statusCode + ") during image pull.");
cb(-1);
}
else
{
var req_entity =
{
Image: image,
NetworkMode: "host",
Cmd: cmd
};
console.log("Creating container...");
options.path = "/containers/create";
var req: http.ClientRequest = https.request(options, function (res: http.ClientResponse)
{
var res_entity: Array<Buffer> = [];
res.on("data", function(data: Buffer)
{
res_entity.push(data);
});
res.on("error", function(e: Error)
{
console.error(e);
cb(-1);
});
res.on("end", function()
{
if (res.statusCode < 200 || res.statusCode >= 300)
{
console.error("Received invalid status code (" + res.statusCode + ") during container creation.");
console.error(Buffer.concat(res_entity).toString());
cb(-1);
}
else
{
var resp_obj: any = {};
try
{
resp_obj = JSON.parse(Buffer.concat(res_entity).toString());
}
catch (ex)
{
console.error(ex);
resp_obj = {};
}
var container_id: string = resp_obj.Id;
console.log("Starting container...");
options.path = "/containers/" + container_id + "/start";
var req: http.ClientRequest = https.request(options, function (res: http.ClientResponse)
{
res.on("error", function(e: Error)
{
console.error(e);
cb(-1);
});
res.on("data", function(data: Buffer) {});
res.on("end", function()
{
if (res.statusCode < 200 || res.statusCode >= 300)
//.........这里部分代码省略.........
示例2:
////////////////////////////////////////////////////
/// Https tests : http://nodejs.org/api/https.html
////////////////////////////////////////////////////
namespace https_tests {
var agent: https.Agent = new https.Agent({
keepAlive: true,
keepAliveMsecs: 10000,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100
});
var agent: https.Agent = https.globalAgent;
https.request({
agent: false
});
https.request({
agent: agent
});
https.request({
agent: undefined
});
}
////////////////////////////////////////////////////
/// TTY tests : http://nodejs.org/api/tty.html
////////////////////////////////////////////////////
namespace tty_tests {
let rs: tty.ReadStream;
示例3: function
res.on("end", function()
{
if (res.statusCode < 200 || res.statusCode >= 300)
{
console.error("Received invalid status code (" + res.statusCode + ") while starting container.");
cb(-1);
}
else
{
console.log("Waiting container...");
options.path = "/containers/" + container_id + "/wait";
var req: http.ClientRequest = https.request(options, function (res: http.ClientResponse)
{
var res_entity: Array<Buffer> = [];
res.on("data", function(data: Buffer)
{
res_entity.push(data);
});
res.on("error", function(e: Error)
{
console.error("Wait response error");
console.error(e);
cb(-1);
});
res.on("end", function()
{
if (res.statusCode < 200 || res.statusCode >= 300)
{
console.error("Received invalid status code (" + res.statusCode + ") while waiting container.");
cb(-1);
}
else
{
var resp_obj: any = {};
try
{
resp_obj = JSON.parse(Buffer.concat(res_entity).toString());
}
catch (ex)
{
console.error(ex);
resp_obj = {};
}
if (typeof resp_obj.StatusCode !== "number")
{
console.error("Received invalid response from container wait.");
cb(-1);
}
else
{
cb(resp_obj.StatusCode);
}
}
});
});
req.setSocketKeepAlive(true, 1000);
req.on("error", function(e: Error)
{
console.error("Wait request error");
console.log(e);
cb(-1);
});
req.end();
}
});
示例4:
/// Https tests : http://nodejs.org/api/https.html ///
//////////////////////////////////////////////////////
namespace https_tests {
var agent: https.Agent = new https.Agent({
keepAlive: true,
keepAliveMsecs: 10000,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100
});
var agent: https.Agent = https.globalAgent;
https.request({
agent: false
});
https.request({
agent: agent
});
https.request({
agent: undefined
});
https.request('http://www.example.com/xyz');
}
////////////////////////////////////////////////////
/// TTY tests : http://nodejs.org/api/tty.html
////////////////////////////////////////////////////
示例5: doRequest
protected doRequest(options: any, callback: (response: any) => void): any {
return https.request(options, callback)
}
示例6: sendRequest
export function sendRequest(options, callback, onData) {
let request, keepAlive, timeout = options.timeout || 5000,
data = options.data;
keepAlive = options.headers && options.headers.Connection === 'Keep-Alive';
// For non streaming connections, use HTTP Agent to pool persistent TCP sockets for HTTP requests
if (!keepAlive) {
if (!options.secure) {
if (!httpAgent) {
httpAgent = new http.Agent({
maxSockets: maxSockets
});
}
options.agent = httpAgent;
}
else {
if (!httpsAgent) {
httpsAgent = new https.Agent({
maxSockets: maxSockets
});
}
options.agent = httpsAgent;
}
Object.keys(options.agent.requests).forEach(function (connectionName) {
// log.info('HTTP', `Socket pool for ${connectionName} has ${options.agent.requests[connectionName].length} pending requests over ${options.agent.sockets[connectionName].length} sockets`);
});
}
// console.info('[INFO] HTTPS OUT', options.hostname, options.port, options.method, options.path);
if (options.secure === false) {
request = http.request(options);
}
else {
request = https.request(options);
}
if (data) {
if (options.headers && options.headers['Content-Type'] === 'application/x-www-form-urlencoded') {
request.write(querystring.stringify(data));
}
else {
request.write(JSON.stringify(data));
}
}
request.end();
request.once('response', options.onResponse || function (response) {
var body = '', statusCode = response.statusCode;
response.on('data', function (chunk) {
if (onData)
onData(chunk);
else {
body += chunk;
}
});
response.once('end', function () {
if (body) {
try {
body = JSON.parse(body);
}
catch (error) {
console.warn('[WARN] HTTPS IN ', options.hostname, options.port, options.method, options.path, body.length, 'Could not parse response body');
console.warn(body);
}
}
if (statusCode !== 200 && statusCode !== 204 && statusCode !== 206) {
// console.error('[ERROR] HTTPS IN ', options.hostname, options.port, options.method, options.path, ':', statusCode, body.length);
return callback(true, body, statusCode, body); // TODO added body as second argument anyway (error responses can have a body that describes the error). Get rid of anywhere expecting it as 4th arg
}
// console.info('[INFO] HTTPS IN ', options.hostname, options.port, options.method, options.path);
// if (options.agent) {
// Object.keys(options.agent.requests).forEach(function (connectionName) {
// console.info('[INFO] Socket pool for', connectionName, 'has', options.agent.requests[connectionName].length, 'pending requests over', options.agent.sockets[connectionName].length, 'sockets');
// });
// }
callback(null, body, statusCode);
});
response.once('error', function (error) {
// console.error('[ERROR] HTTPS IN ', options.hostname, options.port, options.method, options.path, 'Response stream errored', error);
});
request.removeAllListeners();
});
request.on('error', options.onError || function (error) {
console.error('[ERROR] HTTPS IN ', options.hostname, options.port, options.method, options.path, error);
callback(error, null, 500);
});
if (!keepAlive) {
request.setTimeout(timeout, function () {
request.removeAllListeners();
// console.error('[ERROR] HTTPS IN ', options.hostname, options.port, options.method, options.path, 'Timed out after ' + (timeout / 1000) + 's');
callback('timeout', null, 508);
});
}
return request;
}
示例7: catch
return cancellationToken.createPromise((resolve, reject, onCancel) => {
const request = https.request(options, (response: IncomingMessage) => {
try {
this.handleResponse(response, options, cancellationToken, resolve, reject, redirectCount, requestProcessor)
}
catch (e) {
reject(e)
}
})
this.addTimeOutHandler(request, reject)
request.on("error", reject)
requestProcessor(request, reject)
onCancel(() => request.abort())
})
示例8: Buffer
keepAlive: true,
keepAliveMsecs: 10000,
maxSockets: Infinity,
maxFreeSockets: 256
});
var agent: http.Agent = http.globalAgent;
http.request('http://www.example.com/xyz');
}
////////////////////////////////////////////////////
/// Https tests : http://nodejs.org/api/https.html
////////////////////////////////////////////////////
namespace https_tests {
https.request('http://www.example.com/xyz');
}
////////////////////////////////////////////////////
/// Dgram tests : http://nodejs.org/api/dgram.html
////////////////////////////////////////////////////
var ds: dgram.Socket = dgram.createSocket("udp4", (msg: Buffer, rinfo: dgram.RemoteInfo): void => {
});
var ai: dgram.AddressInfo = ds.address();
ds.send(new Buffer("hello"), 0, 5, 5000, "127.0.0.1", (error: Error, bytes: number): void => {
});
////////////////////////////////////////////////////
///Querystring tests : https://gist.github.com/musubu/2202583
////////////////////////////////////////////////////
示例9: function
function (resolve, reject) {
var item = url.parse(path);
var postData = JSON.stringify(obj);
var newHeader = {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
Object.assign(newHeader, headers);
var options: https.RequestOptions = {
host: item.hostname,
port: +item.port,
path: item.path,
method: 'POST',
headers: newHeader,
}
if (agent != null) {
options.agent = agent;
}
if (item.protocol.startsWith('https:')) {
var req = https.request(options, function (res) {
if (res.statusCode !== 200) {
//reject();
//return;
}
var result = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
result += chunk;
});
res.on('end', function () {
resolve(result);
});
res.on('error', function (e) {
reject(e);
});
});
req.write(postData);
req.end();
} else {
var req = http.request(options, function (res) {
var result = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
result += chunk;
});
res.on('end', function () {
resolve(result);
});
res.on('error', function (e) {
reject(e);
});
});
req.write(postData);
req.end();
}
}
示例10:
//////////////////////////////////////////////////////
{
let agent: https.Agent = new https.Agent({
keepAlive: true,
keepAliveMsecs: 10000,
maxSockets: Infinity,
maxFreeSockets: 256,
maxCachedSessions: 100,
timeout: 15000
});
agent = https.globalAgent;
https.request({
agent: false
});
https.request({
agent
});
https.request({
agent: undefined
});
https.get('http://www.example.com/xyz');
https.request('http://www.example.com/xyz');
https.get('http://www.example.com/xyz', (res: http.IncomingMessage): void => {});
https.request('http://www.example.com/xyz', (res: http.IncomingMessage): void => {});
https.get(new url.URL('http://www.example.com/xyz'));