(url, options = {})
| 211 | } |
| 212 | |
| 213 | const http = function (url, options = {}) { |
| 214 | if(options.method ==='POST' && options.data){ |
| 215 | options.body = JSON.stringify(options.data); |
| 216 | options.headers = Object.assign({'content-type':'application/json'}, options.headers); |
| 217 | } |
| 218 | options.timeout = request_timeout; |
| 219 | if(!options.headers){ |
| 220 | options.headers = {}; |
| 221 | } |
| 222 | let keys = Object.keys(options.headers).map(it=>it.toLowerCase()); |
| 223 | if(!keys.includes('referer')){ |
| 224 | options.headers['Referer'] = getHome(url); |
| 225 | } |
| 226 | if(!keys.includes('user-agent')){ |
| 227 | options.headers['User-Agent'] = UA; |
| 228 | } |
| 229 | console.log(JSON.stringify(options.headers)); |
| 230 | try { |
| 231 | const res = req(url, options); |
| 232 | // if(options.headers['Authorization']){ |
| 233 | // console.log(res.content); |
| 234 | // } |
| 235 | res.json = () => res&&res.content ? JSON.parse(res.content) : null; |
| 236 | res.text = () => res&&res.content ? res.content:''; |
| 237 | return res |
| 238 | }catch (e) { |
| 239 | return { |
| 240 | json() { |
| 241 | return null |
| 242 | }, text() { |
| 243 | return '' |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | }; |
| 248 | ["get", "post"].forEach(method => { |
| 249 | http[method] = function (url, options = {}) { |
| 250 | return http(url, Object.assign(options, {method: method.toUpperCase()})); |
no test coverage detected