(url, jsonData, needBinary, httpOptions, method)
| 388 | } |
| 389 | |
| 390 | function postHttp (url, jsonData, needBinary, httpOptions, method) { |
| 391 | return new Promise((accept, reject) => { |
| 392 | const parsed = new URL(url); |
| 393 | const protocol = (parsed.protocol === 'https:' ? https : http); |
| 394 | try { |
| 395 | const postData = JSON.stringify(jsonData); |
| 396 | const options = Object.assign({ |
| 397 | host: parsed.host, |
| 398 | path: parsed.pathname + parsed.search, |
| 399 | method: (method || 'POST'), |
| 400 | headers: { |
| 401 | 'Content-Type': 'application/json', |
| 402 | 'Content-Length': Buffer.byteLength(postData), |
| 403 | } |
| 404 | }, httpOptions || {}); |
| 405 | |
| 406 | const req = protocol.request(options, (res) => { |
| 407 | toUtf8(res, (contentType, content) => { |
| 408 | accept({statusCode: res.statusCode, statusMessage: res.statusMessage, content, contentType}); |
| 409 | }, needBinary); |
| 410 | }).on('error', reject); |
| 411 | req.write(postData); |
| 412 | req.end(); |
| 413 | } catch (e) { |
| 414 | console.error('Cannot fetch', url, e); |
| 415 | reject(e); |
| 416 | } |
| 417 | }); |
| 418 | } |
| 419 | |
| 420 | function fetchHttp (url, needBinary, httpOptions) { |
| 421 | return new Promise((accept, reject) => { |
no test coverage detected