How to use urllib

Comprehensive urllib code examples:

How to use urllib.curl:

84
85
86
87
88
89
90
91
92
93
if (!stdout.includes(`started on ${appUrl}`)) {
  throw new Error('app start not success');
}

// check server status
const resp = await urllib.curl(appUrl);
assert(resp.status === 200, 'app start not success');
console.info('app started success in ' + appUrl);

// update nginx config

How to use urllib.HttpClient:

9
10
11
12
13
14
15
16
17
18
19
20
module.exports = get;


const USER_AGENT = 'npminstall/' + require('../package.json').version + ' ' + urllib.USER_AGENT;
const MAX_RETRY = 5;
const cacheable = new CacheableLookup();
const httpclient = new urllib.HttpClient({
  lookup: cacheable.lookup,
});


async function get(url, options, globalOptions, hasCache = false) {

How to use urllib.create:

22
23
24
25
26
27
28
29
30
31
options = options || {};
this.name = options.name || 'egg-init';
this.configName = options.configName || 'egg-init-config';
this.pkgInfo = options.pkgInfo || require('../package.json');
this.needUpdate = options.needUpdate !== false;
this.httpClient = urllib.create();

this.inquirer = inquirer;
this.fileMapping = {
  gitignore: '.gitignore',

How to use urllib.USER_AGENT:

6
7
8
9
10
11
12
13
14
15
16
17
const utils = require('./utils');
const cnpmConfig = require('./cnpm_config');


module.exports = get;


const USER_AGENT = 'npminstall/' + require('../package.json').version + ' ' + urllib.USER_AGENT;
const MAX_RETRY = 5;
const cacheable = new CacheableLookup();
const httpclient = new urllib.HttpClient({
  lookup: cacheable.lookup,

How to use urllib.request:

173
174
175
176
177
178
179
180
181
    data.stream = requestForm;
} else {
    data.headers['Content-Length'] = 0;
}

var req = urllib.request(requestURI, data, function (respErr, respBody,
                                                     respInfo) {
    callbackFunc(respErr, respBody, respInfo);
});