How to use the request function from http

Find comprehensive JavaScript http.request code examples handpicked from public code repositorys.

12
13
14
15
16
17
18
19
20
21
                        this.req = https.request(options);
        } else if (hasCrypto && "https:" == options.protocol) {
                this.req = https.request(options);
        }
        if (!this.req)
                this.req = http.request(options);
}

ClientRequest.prototype.on = function(evt, callback) {
        this.req.on(evt, callback);
fork icon988
star icon0
watch icon3

+ 11 other calls in file

27
28
29
30
31
32
33
34
35
36
  })
}

// parse out the options from options.url
var opts = url.parse(options.url)
var method = opts.protocol === 'https:' ? https.request : http.request
var defaultContentType = 'application/json; charset=utf-8'

// check for additional query params
if (options.data) {
fork icon18
star icon165
watch icon0

347
348
349
350
351
352
353
354
355
356
+  maxSockets: 1
+});
+
+function request(path, callback) {
+  const port = server.address().port;
+  const req = http.request({ agent, path, port }, common.mustCall((res) => {
+    assert.strictEqual(res.statusCode, 200);
+
+    res.setEncoding('utf8');
+
fork icon11
star icon0
watch icon1

66
67
68
69
70
71
72
73
74
75

exports.request = function(options, cb) {
  if (options.agent === undefined) {
    options.agent = globalAgent;
  }
  return http.request(options, cb);
};

exports.get = function(options, cb) {
  options.method = 'GET';
fork icon0
star icon3
watch icon28

40
41
42
43
44
45
46
47
48
49
this.rpcReq.params = params || [];
var postData = JSON.stringify(this.rpcReq);

var timeoutHandle = null;

var req = http.request({
    method: 'POST',
    hostname: this.url,
    port: this.port,
    path: this.path,
fork icon0
star icon1
watch icon2

7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
result.query = relative.query;
result.host = relative.host || '';
result.auth = relative.auth;
result.hostname = relative.hostname || relative.host;
result.port = relative.port;
// to support http.request
if (result.pathname || result.search) {
  var p = result.pathname || '';
  var s = result.search || '';
  result.path = p + s;
fork icon0
star icon0
watch icon1

+ 24 other calls in file

449
450
451
452
453
454
455
456
457
458

// https if specified, fallback to http in any other case
if (options.protocol == 'https:') {
  request = https.request(options);
} else {
  request = http.request(options);
}

// get content length and fire away
this.getLength(function(err, length) {
fork icon0
star icon0
watch icon1

+ 10 other calls in file

322
323
324
325
326
327
328
329
330
331
      if(data.sector == "Office"){
        var options = {
          host: '10.168.1.64',
          path: '/relay/0?turn=on'
        };
        http.request(options, callback).end();
      }
    //}
  },
}
fork icon0
star icon0
watch icon0

701
702
703
704
705
706
707
708
709
710
  }
}

const defaultPort = isSecure ? 443 : 80;
const key = randomBytes(16).toString('base64');
const request = isSecure ? https.request : http.request;
const protocolSet = new Set();
let perMessageDeflate;

opts.createConnection = isSecure ? tlsConnect : netConnect;
fork icon0
star icon0
watch icon1

138
139
140
141
142
143
144
145
146
147
  headers: {
       'Content-Type': 'application/x-www-form-urlencoded',
       'Content-Length': data.length
     }
};
let request = http.request(options, (response) => {
    response.setEncoding('utf8');
    let rawData = '';
    response.on('data', (chunk) => { rawData += chunk; });
    response.on('end', () => {
fork icon0
star icon0
watch icon2

379
380
381
382
383
384
385
386
387
388
{
  host: 'www.haus-bus.de',
  path: '/'+firmwareName+'.chk'
};

http.request(options, function(response) 
{
  var str = '';
  response.on('data', function (chunk) {str += chunk;});
  response.on('end', function () 
fork icon0
star icon0
watch icon2

29
30
31
32
33
34
35
36
37
38
if (req.query.user) {
    opt.path = opt.path + "&user=" + req.query.user;
}
console.log(opt.path);
var body = '';
var request = http.request(opt, function (response) {
    console.log("Got response: " + response.statusCode);
    response.on('data', function (d) {
        body += d;
    }).on('end', function () {
fork icon0
star icon0
watch icon1

32
33
34
35
36
37
38
39
40
41
  hostname: 'ip-api.com',
  path: '/json',
  method: 'GET'
};

const req2 = http.request(options, res2 => {
  let body = '';
  res2.on('data', chunk => {
    body += chunk;
  });
fork icon0
star icon0
watch icon0

143
144
145
146
147
148
149
150
151
152
(r) => {
	let buffers = [];
	r.on("data", (b) => buffers.push(b));
	r.on("end", () => {
		const nonce = JSON.parse(Buffer.concat(buffers)).nonce;
		let req = http.request(
			{
				hostname: "acapela-group.com",
				port: "8080",
				path: "/webservices/1-34-01-Mobility/Synthesizer",
fork icon0
star icon0
watch icon0

732
733
734
735
736
737
738
739
740
}
if (self._aws) {
  self.aws(self._aws, true)
}

// We have a method named auth, which is completely different from the http.request
// auth option.  If we don't remove it, we're gonna have a bad time.
var reqOptions = copy(self)
delete reqOptions.auth
fork icon0
star icon0
watch icon0

11
12
13
14
15
16
17
18
19
20
21
22
const jp = require('jsonpath');
const csv_stringify = require('csv-stringify/lib/sync');


const run_query = (query_options, filters, callback) => {
    let tic = () => 0.;
    http.request(query_options, function (res) {
        let body = '', ttfb = tic();


        res.setEncoding('utf8');
        res.on('data', function (chunk) {
fork icon0
star icon0
watch icon0

297
298
299
300
301
302
303
304
305
306
You can start writing to the stream right away and the writes will be buffered
until the `'connect'` event fires.

## net.createServer()

## http.request()

## http.createServer()

## zlib.createGzip()
fork icon0
star icon0
watch icon2