-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.js
47 lines (39 loc) · 1.36 KB
/
request_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var http = require('http');
var https = require('https');
var url = require('url');
var util = require('util');
var fs = require('fs')
var argUrl = process.argv[2];
var parsedUrl = url.parse(argUrl, true);
var options = {host: null, port: -1, path: null, method: 'GET'};
options.host = parsedUrl.hostname;
options.port = parsedUrl.port;
options.path = parsedUrl.pathname;
if (parsedUrl.search) options.path += "?" + parsedUrl.search;
var req = https.request(options, function(res){
util.log('STATUS: ' + res.statusCode);
util.log('HEADERS: ' + util.inspect(res.headers));
var messages = "";
var totalSize = 0;
res.setEncoding('binary');
res.on('data', function(chunk){
totalSize += chunk.length;
messages+=chunk;
});
res.on('error', function(err){
util.log('RESPONSE ERROR: ' + err);
});
res.on('end', function(){
fs.writeFile('a.jpg', messages, 'binary', function(err) {
if (err) {
util.log('error...');
}
util.log('it\'s saved..totalSize:'+totalSize);
});
});
});
req.on('error', function(err){
util.log('REQUEST ERROR: ' + err);
});
req.end();
//https://images.unsplash.com/photo-1473642676276-2d4ab561542e?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&s=fda6c92b02daf700051628cd6d6d8565