How to use the request function from urllib

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

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);
});
fork icon188
star icon578
watch icon0

+ 8 other calls in file

74
75
76
77
78
79
80
81
82
83
84


async function _get(url, options, retry, globalOptions, hasCache) {
  try {
    // let mock agent easy for unittest
    if (process.env.MOCK_AGENT) {
      return await urllib.request(url, options);
    }
    return await httpclient.request(url, options);
  } catch (err) {
    retry--;
fork icon64
star icon476
watch icon17

+ 12 other calls in file

22
23
24
25
26
27
28
29
30
31
32
    });
}


module.exports.syncFetch = async function (url, referer, timeout) {
    try {
        const {data} = await urllib.request(url, {
            method: 'GET',
            timeout: timeout,
            dataType: 'json',
            headers: {
fork icon52
star icon337
watch icon0

281
282
283
284
285
286
287
288
289
290

var fileName = req.docManager.getCorrectName(title);
var userAddress = req.docManager.curUserHostAddress();
req.docManager.historyPath(fileName, userAddress, true);

urllib.request(fileUrl, {method: "GET"},function(err, data) {
    if (configServer.get("maxFileSize") < data.length || data.length <= 0) {  // check if the file size exceeds the maximum file size
        res.writeHead(200, { "Content-Type": "application/json" });
        res.write(JSON.stringify({ "error": "File size is incorrect" }));
        res.end();
fork icon84
star icon95
watch icon37

+ 4 other calls in file

118
119
120
121
122
123
124
125
126
127
}
args.sign = this.sign(args);
type = type || 'GET';
var options = {type: type, data: args, agent: this.agent};
var that = this;
urllib.request(that.REST_URL, options, function (err, buffer) {
  var data;
  if (buffer) {
    buffer = that._wrapJSON(buffer.toString());
    try {
fork icon66
star icon143
watch icon31

1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
if (err) {
  return callback(err);
}
url = hostname + url;
var body = params.join('&');
urllib.request(url, {
  content: body,
  type: 'POST',
  timeout: self.requestTimeout,
  agent: self.requestAgent
fork icon30
star icon49
watch icon17

+ 5 other calls in file

439
440
441
442
443
444
445
446
447
448
    writeStream: file,
    consumeWriteStream: true,
    followRedirect: true
};
log.trace(`Download Options: ${JSON.stringify(dlOptions)}`);
urllib.request(dlURL.toString(), dlOptions, function (err, data, res) {
    log.info('Download finished, installing Visual Studio Code extension');
    let installOutput;
    try {
        installOutput = execSync(`code --install-extension ${filename} --force`,
fork icon15
star icon27
watch icon0

119
120
121
122
123
124
125
126
127
128
  data: data,
  headers: {},
};
var req = {url: url, params: params};
req = self.setAuthentication(req);
urllib.request(req.url, req.params, function (err, resData, res) {
  debug('%s %s %j: status: %s, resData: %d bytes, err: %j',
    method, url, data, res && res.statusCode, resData && resData.length || 0, err);

  self.handleResult(method, err, resData, res, function (err, result) {
fork icon6
star icon6
watch icon10

+ 29 other calls in file

138
139
140
141
142
143
144
145
146
147
        "Content-Type": "application/json",
        "charset": "utf-8"
    }
};
// console.log('pkg', JSON.stringify(pkg))
urllib.request(URLS_SEND + appid, pkg, function (err, data, res) {
    if (err) return callback(err);
    // console.log(data.toString());
    that.validate(data.toString(), appid, callback);
});
fork icon1
star icon0
watch icon0

86
87
88
89
90
91
92
93
94
95
    }
  }
}

async _getHTML(url) {
  const ret = await urllib.request(url, {
    timeout: 30 * 1000,
  });
  if (ret.status >= 300) {
    return { status: ret.status };
fork icon0
star icon4
watch icon2

166
167
168
169
170
171
172
    agentid: this.agentid
  };
  extend(data, to);
  extend(data, message);

  urllib.request(url, postJSON(data), wrapper(callback));
};
fork icon147
star icon0
watch icon1

173
174
175
176
177
178
179
180
181
182
  method: 'receive_tx',
  params: [slate, null, null],
}
res = await axios.post(url, payload)
//console.log('payload', JSON.stringify(payload))
//res = await urllib.request(url, {
//  method: 'post',
//  contentType: "application/json",
//  dataType: 'json',
//  timeout: '25s',
fork icon29
star icon82
watch icon10

15
16
17
18
19
20
21
22
23
24
exports.request = async function (url, options = {}) {
  return new Promise((resolve, reject) => {
    const headers = {
      'X-Auth-Token': process.env.YUE_TOKEN
    };
    urllib.request(url, { headers, timeout: 30000, ...options }, function (err, data, res) {
      if (err) {
        reject(err)
      } else {
        resolve(data)
fork icon2
star icon5
watch icon3

153
154
155
156
157
158
159
160
161
162
// npm don't support range now
if (semver.validRange(tag)) {
  url = `${this.options.registry}/${name}`;
  isAllVersions = true;
}
const res = yield urllib.request(url, {
  headers: {
    'user-agent': USER_AGENT,
    // npm will response less data
    accept: 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*',
fork icon18
star icon205
watch icon0

77
78
79
80
81
82
83
84
85
86
  dataType: 'json',
  data: data
};

var url = PREFIX + 'singlesend?t=ajax-response&lang=zh_CN';
urllib.request(url, options, function (err, body, res) {
  if (err) {
    return callback(err);
  }
  if (body.ret !== '0') {
fork icon27
star icon39
watch icon0

105
106
107
108
109
110
111
112
113
114
    type: type,
    data: args,
    agent: this.agent
};
var that = this;
urllib.request(that.REST_URL, options, function(err, buffer) {
    var data;
    if (buffer) {
        buffer = that._wrapJSON(buffer.toString());
        try {
fork icon2
star icon0
watch icon2

20
21
22
23
24
25
26
27
28
29
30
};
const ANTI_REPLAY_ALLOW_SECONDS_RANGE = 600;
const DEFAULT_REG_INTERVAL_SECONDS = 30;
const DEFAULT_CENTER_SERVER_URL = 'http://api.contoso.com';


const defaultHttpRequestFunc = async (url, options) => await urllib.request(url, options);
const defaultLogFunc = (level, log) => {
  switch (level) {
    case 'warn': return console.warn(log);
    case 'error': return console.error(log);
fork icon1
star icon2
watch icon1

32
33
34
35
36
37
38
39
40
        data.stream = form;
    } else {
        data.headers['Content-Length'] = 0;
    };

    var req = urllib.request(url, data, callback);

    return req;
}
fork icon1
star icon0
watch icon0

73
74
75
76
77
78
79
80
81
82
var openid = data.openid
var unionid = data.unionid
var persistent_code = data.persistent_code
if (!openid || !persistent_code) return callback(new Error('getPersistentCode failed'))

urllib.request(self._snsTokenURL + '?access_token=' + self.access_token, {
  headers: {
    'Content-Type': 'application/json'
  },
  method: 'POST',
fork icon1
star icon0
watch icon0

+ 2 other calls in file

46
47
48
49
50
51
52
53
54
55
  data.stream = form;
} else {
  data.headers['Content-Length'] = 0;
};

var req = urllib.request(uri, data, function(err, result, res) {
  var rerr = null;
  if (err || Math.floor(res.statusCode/100) !== 2) {
    rerr = {code: res&&res.statusCode||-1, error: err||result.error||''};
  }
fork icon0
star icon2
watch icon0