Comparing version 1.1.5 to 1.2.0
@@ -12,7 +12,2 @@ const http = require('http'); | ||
//设置此消息头,可以覆盖掉默认选项,可以添加 | ||
this.headers = { | ||
}; | ||
this.mime_map = { | ||
@@ -86,3 +81,3 @@ 'css' : 'text/css', | ||
this.get = function(url, callback) { | ||
this.get = function(url, callback, encoding='utf8') { | ||
var opts = this.parseUrl(url); | ||
@@ -107,7 +102,7 @@ opts.method = 'GET'; | ||
res.setEncoding('utf8'); | ||
res.setEncoding(encoding); | ||
var get_data = ''; | ||
res.on('data', (data) => { | ||
get_data += data.toString(); | ||
get_data += data.toString(encoding); | ||
}); | ||
@@ -130,4 +125,15 @@ | ||
/* | ||
options = { | ||
data, | ||
headers, | ||
encoding | ||
} | ||
*/ | ||
this.post = function(url, data, callback) { | ||
this.post = function(url, options, callback) { | ||
if (options.encoding === undefined) { | ||
options.encoding = 'utf8'; | ||
} | ||
var opts = this.parseUrl(url); | ||
@@ -140,13 +146,15 @@ var h = (opts.protocol === 'https:') ? https : http; | ||
for(var k in this.headers) { | ||
opts.headers[k] = this.headers[k]; | ||
if (options.headers !== undefined) { | ||
for(var k in options.headers) { | ||
opts.headers[k] = this.headers[k]; | ||
} | ||
} | ||
if (opts.headers['Content-Type'] === 'application/x-www-form-urlencoded') { | ||
post_data = qs.stringify(data); | ||
post_data = qs.stringify(options.data); | ||
} else { | ||
if (typeof data === 'object') { | ||
post_data = JSON.stringify(data); | ||
if (typeof options.data === 'object') { | ||
post_data = JSON.stringify(options.data); | ||
} else { | ||
post_data = data; | ||
post_data = options.data; | ||
} | ||
@@ -160,5 +168,5 @@ } | ||
res.setEncoding('utf8'); | ||
res.setEncoding(options.encoding); | ||
res.on('data', (data) => { | ||
res_data += data.toString(); | ||
res_data += data.toString(options.encoding); | ||
}); | ||
@@ -245,3 +253,2 @@ | ||
} | ||
console.log(formData); | ||
} | ||
@@ -299,2 +306,19 @@ | ||
/* | ||
options = { | ||
method : GET | POST, | ||
data : Object (if method == POST) | ||
} | ||
*/ | ||
this.download = function(url, options, callback) { | ||
if (options.method == 'GET') { | ||
return this.get(url, callback, 'binary'); | ||
} else if (options.method == 'POST') { | ||
return this.post(url, { | ||
data : options.data, | ||
encoding : 'binary' | ||
}, callback); | ||
} | ||
}; | ||
}; | ||
@@ -301,0 +325,0 @@ |
{ | ||
"name": "ant-http", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"description": "simple http/https request lib", | ||
@@ -5,0 +5,0 @@ "main": "anthttp.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9975
261