Commit 56d4cc6c authored by antoor's avatar antoor

Add custom headers&&body features

增加自定义HTTP头&&请求数据功能
parent d63bd1b3
...@@ -93,16 +93,23 @@ class Request { ...@@ -93,16 +93,23 @@ class Request {
onRequest(event, opts) { onRequest(event, opts) {
logger.debug('onRequest::opts', opts); logger.debug('onRequest::opts', opts);
superagent const _request = superagent.post(opts['url']);
.post(opts['url']) // 设置headers
.set('User-Agent', USER_AGENT) _request.set('User-Agent', USER_AGENT);
// 自定义headers
for (let _ in opts.headers) {
_request.set(_, opts.headers[_]);
}
// 自定义body
const _postData = Object.assign({}, opts.body, opts.data);
_request
.proxy(APROXY_CONF['uri']) .proxy(APROXY_CONF['uri'])
.type('form') .type('form')
// 超时 // 超时
.timeout(REQ_TIMEOUT) .timeout(REQ_TIMEOUT)
// 忽略HTTPS // 忽略HTTPS
.ignoreHTTPS(opts['ignoreHTTPS']) .ignoreHTTPS(opts['ignoreHTTPS'])
.send(opts['data']) .send(_postData)
.parse((res, callback) => { .parse((res, callback) => {
this.parse(opts['tag_s'], opts['tag_e'], (chunk) => { this.parse(opts['tag_s'], opts['tag_e'], (chunk) => {
event.sender.send('request-chunk-' + opts['hash'], chunk); event.sender.send('request-chunk-' + opts['hash'], chunk);
...@@ -139,17 +146,23 @@ class Request { ...@@ -139,17 +146,23 @@ class Request {
let indexEnd = -1; let indexEnd = -1;
let tempData = []; let tempData = [];
// 开始HTTP请求 const _request = superagent.post(opts['url']);
superagent // 设置headers
.post(opts['url']) _request.set('User-Agent', USER_AGENT);
.set('User-Agent', USER_AGENT) // 自定义headers
for (let _ in opts.headers) {
_request.set(_, opts.headers[_]);
}
// 自定义body
const _postData = Object.assign({}, opts.body, opts.data);
_request
.proxy(APROXY_CONF['uri']) .proxy(APROXY_CONF['uri'])
.type('form') .type('form')
// 设置超时会导致文件过大时写入出错 // 设置超时会导致文件过大时写入出错
// .timeout(timeout) // .timeout(timeout)
// 忽略HTTPS // 忽略HTTPS
.ignoreHTTPS(opts['ignoreHTTPS']) .ignoreHTTPS(opts['ignoreHTTPS'])
.send(opts['data']) .send(_postData)
.pipe(through( .pipe(through(
(chunk) => { (chunk) => {
// 判断数据流中是否包含后截断符?长度++ // 判断数据流中是否包含后截断符?长度++
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment