diff --git a/src/utils/api-request.ts b/src/utils/api-request.ts index 9616b7a39f..15632b1671 100644 --- a/src/utils/api-request.ts +++ b/src/utils/api-request.ts @@ -22,7 +22,6 @@ import * as validator from './validator'; import http = require('http'); import https = require('https'); import http2 = require('http2'); -import url = require('url'); import { EventEmitter } from 'events'; import { Readable } from 'stream'; import * as zlibmod from 'zlib'; @@ -974,23 +973,23 @@ class BaseRequestConfigImpl implements BaseRequestConfig { return data; } - protected buildUrl(): url.UrlWithStringQuery { + protected buildUrl(): URL { const fullUrl: string = this.urlWithProtocol(); + const parsedUrl = new URL(fullUrl); if (!this.hasEntity() || this.isEntityEnclosingRequest()) { - return url.parse(fullUrl); + return parsedUrl; } if (!validator.isObject(this.data)) { throw new Error(`${this.method} requests cannot have a body`); } - // Parse URL and append data to query string. - const parsedUrl = new url.URL(fullUrl); + // Append data to query string. const dataObj = this.data as {[key: string]: string}; for (const key in dataObj) { if (Object.prototype.hasOwnProperty.call(dataObj, key)) { parsedUrl.searchParams.append(key, dataObj[key]); } } - return url.parse(parsedUrl.toString()); + return parsedUrl; } protected urlWithProtocol(): string { @@ -1027,7 +1026,7 @@ class HttpRequestConfigImpl extends BaseRequestConfigImpl implements HttpRequest public buildRequestOptions(): https.RequestOptions { const parsed = this.buildUrl(); const protocol = parsed.protocol; - let port: string | null = parsed.port; + let port: string = parsed.port; if (!port) { const isHttps = protocol === 'https:'; port = isHttps ? '443' : '80'; @@ -1037,7 +1036,7 @@ class HttpRequestConfigImpl extends BaseRequestConfigImpl implements HttpRequest protocol, hostname: parsed.hostname, port, - path: parsed.path, + path: `${parsed.pathname}${parsed.search}`, method: this.method, agent: this.httpAgent, headers: Object.assign({}, this.headers), @@ -1065,7 +1064,7 @@ class Http2RequestConfigImpl extends BaseRequestConfigImpl implements Http2Reque return { protocol, - path: parsed.path, + path: `${parsed.pathname}${parsed.search}`, method: this.method, headers: Object.assign({}, this.headers), };