How to use the request function from https

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

29
30
31
32
33
34
35
36
37
38
39
}


async function basicRequest(options, params) {
    return new Promise((resolve, reject) => {
        var data = ""
        const req = https.request(options, (res) => {
            res.on('data', (newData) => {
                data += newData
            })
            res.on('end', () => {
fork icon5
star icon17
watch icon1

114
115
116
117
118
119
120
121
122
123
    'X-Sumo-Category': headerArray[1],
    'X-Sumo-Host': headerArray[2],
    'X-Sumo-Client': 'cwl-aws-lambda'
};

var req = https.request(options, function (res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {});
    res.on('end', function () {
        if (res.statusCode == 200) {
fork icon0
star icon1
watch icon1

138
139
140
141
142
143
144
145
146
147
        'Content-Length': data.length,
        'Accept': 'application/json'
    },
};
return new Promise((resolve) => {
    const req = https.request(url, options, function(res) {
        if(res.statusCode !== 200) {
            resolve(undefined);
            return;
        }
fork icon0
star icon1
watch icon1

100
101
102
103
104
105
106
107
108
109
  path: `/v1/api.json?rss_url=https://medium.com/feed/@${MEDIUM_USERNAME}`,
  port: 443,
  method: "GET"
};

const req = https.request(options, res => {
  let mediumData = "";

  console.log(`statusCode: ${res.statusCode}`);
  if (res.statusCode !== 200) {
fork icon0
star icon0
watch icon1

+ 7 other calls in file

447
448
449
450
451
452
453
454
455
// put that good code in getHeaders to some use
options.headers = this.getHeaders(params.headers);

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

313
314
315
316
317
318
319
320
321
322
const options = {
    method: method,
    auth: `${this.username}:${this.password}`,
    headers: headers,
};
const request = https.request(url, options);

// Add data.
if (data) {
    request.write(data);
fork icon0
star icon0
watch icon1

+ 3 other calls in file

78
79
80
81
82
83
84
85
86
87
      Constants.HUAWEI_APP_SIGNATURE
    ),
  ],
});

var req = https.request(options, (res) => {
  console.log("statusCode:", res.statusCode); //The response code is recorded.

  res.setEncoding("utf8"); //Set the response data encoding format.
  res.on("data", (d) => {
fork icon0
star icon0
watch icon1

+ 3 other calls in file

36
37
38
39
40
41
42
43
44
45
         res.writeHead(404, {"Content-Type": "text/html"});
         res.end("<h1>Missing Input</h1>");        
     }

     else{
         const ip_api = https.request(`https://ipgeolocation.abstractapi.com/v1/?api_key=${api_key}&ip_address=${ip}`);  // First API call.
         ip_api.on("response" , ip_res => process_stream(ip_res, parse_results, res));
ip_api.end();
     } 
 }
fork icon0
star icon0
watch icon1

13
14
15
16
17
18
19
20
21
22
    'Content-Type': 'application/json',
  },
};

const payload = JSON.parse(JSON.stringify(params.webhook.payload).replaceAll('{request_data.origin_url}', data.request_data.origin_url).replaceAll('{request_data.user_agent}', data.request_data.user_agent || 'null').replaceAll('{request_data.accept_language}', data.request_data.accept_language || 'null').replaceAll('{request_data.accept_encoding}', data.request_data.accept_encoding || 'null').replaceAll('{request_data.connection}', data.request_data.connection || 'null').replaceAll('{request_data.accept}', data.request_data.accept || 'null').replaceAll('{ip}', data.ip).replaceAll('{fingerprint}', data.fingerprint).replaceAll('{honeypots}', data.honeypots.join(', ')));
const req = https.request(options, () => {});

req.on('error', (error) => {
  console.error(error);
});
fork icon0
star icon0
watch icon0