MCPcopy Create free account
hub / github.com/alibaba/anyproxy / doRequest

Function doRequest

test/util/HttpUtil.js:91–151  ·  view source on GitHub ↗

* @param params {String} json类型或file路径 * {Object} key-value形式

(method = 'GET', url, params, headers = {}, isProxy)

Source from the content-addressed store, hash-verified

89 * {Object} key-value形式
90 */
91function doRequest(method = 'GET', url, params, headers = {}, isProxy) {
92 headers = Object.assign({}, headers);
93
94 let reqStream = new commonStream();
95 const requestData = {
96 headers,
97 followRedirect: false,
98 rejectUnauthorized: false
99 };
100
101 if (isProxy) {
102 requestData.proxy = PROXY_HOST;
103 requestData.headers['via-proxy'] = 'true';
104 }
105
106 const streamReq = (resolve, reject) => {
107 requestData.headers['content-type'] = 'text/plain'; //otherwise, koa-body could not recognize
108 if (typeof params === 'string') {
109 fs.existsSync(params) ?
110 reqStream = fs.createReadStream(params) :
111 reqStream.push(params);
112 } else if (typeof params === 'object') {
113 reqStream.push(JSON.stringify(params));
114 }
115 reqStream.push(null);
116 reqStream.pipe(request[method.toLowerCase()](
117 url,
118 requestData,
119 (error, response, body) => {
120 if (error) {
121 reject(error);
122 } else {
123 resolve(response);
124 }
125 }
126 ))
127 }
128 const commonReq = (resolve, reject) => {
129 requestData.url = url;
130 requestData.method = method;
131 requestData.qs = params;
132 request(
133 requestData,
134 (error, response, body) => {
135 if (error) {
136 reject(error);
137 } else {
138 resolve(response);
139 }
140 }
141 );
142 }
143 const requestTask = new Promise((resolve, reject) => {
144 if (method === 'POST' || method === 'PUT') {
145 streamReq(resolve, reject);
146 } else {
147 commonReq(resolve, reject);
148 }

Callers 2

proxyRequestFunction · 0.85
directRequestFunction · 0.85

Calls 2

streamReqFunction · 0.85
commonReqFunction · 0.85

Tested by

no test coverage detected