(url, post_data, boundary, file, callback, options)
| 562 | } |
| 563 | |
| 564 | function post(url, post_data, boundary, file, callback, options) { |
| 565 | let file_header; |
| 566 | let finish_buffer = Buffer.from("--" + boundary + "--", 'ascii'); |
| 567 | let oauth_token = options.oauth_token || config().oauth_token; |
| 568 | if ((file != null) || options.stream) { |
| 569 | // eslint-disable-next-line no-nested-ternary |
| 570 | let filename = options.stream ? options.filename ? options.filename : "file" : basename(file); |
| 571 | file_header = Buffer.from(encodeFilePart(boundary, 'application/octet-stream', 'file', filename), 'binary'); |
| 572 | } |
| 573 | const parsedUrl = new URL(url); |
| 574 | let post_options = { |
| 575 | protocol: parsedUrl.protocol, |
| 576 | hostname: parsedUrl.hostname, |
| 577 | path: parsedUrl.pathname + parsedUrl.search, |
| 578 | pathname: parsedUrl.pathname, |
| 579 | query: parsedUrl.search ? parsedUrl.search.substring(1) : '' |
| 580 | }; |
| 581 | |
| 582 | if (parsedUrl.port) { |
| 583 | post_options.port = parsedUrl.port; |
| 584 | } |
| 585 | |
| 586 | let headers = { |
| 587 | 'Content-Type': `multipart/form-data; boundary=${boundary}`, |
| 588 | 'User-Agent': utils.getUserAgent() |
| 589 | }; |
| 590 | if (options.content_range != null) { |
| 591 | headers['Content-Range'] = options.content_range; |
| 592 | } |
| 593 | if (options.x_unique_upload_id != null) { |
| 594 | headers['X-Unique-Upload-Id'] = options.x_unique_upload_id; |
| 595 | } |
| 596 | if (options.extra_headers !== null) { |
| 597 | headers = merge(headers, options.extra_headers); |
| 598 | } |
| 599 | if (oauth_token != null) { |
| 600 | headers.Authorization = `Bearer ${oauth_token}`; |
| 601 | } |
| 602 | |
| 603 | post_options = extend(post_options, { |
| 604 | method: 'POST', |
| 605 | headers: headers |
| 606 | }); |
| 607 | if (options.agent != null) { |
| 608 | post_options.agent = options.agent; |
| 609 | } |
| 610 | let proxy = options.api_proxy || config().api_proxy; |
| 611 | if (!isEmpty(proxy)) { |
| 612 | if (!post_options.agent && agent) { |
| 613 | post_options.agent = agent; |
| 614 | } else if (!post_options.agent) { |
| 615 | post_options.agent = new https.Agent(proxy); |
| 616 | } else { |
| 617 | console.warn("Proxy is set, but request uses a custom agent, proxy is ignored."); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | let post_request = https.request(post_options, callback); |
no test coverage detected