url.build

url.build

url.build

Function

Constructs a URL string from an object configuration.

Syntax

Parameters

ParametersTypeRequiredDefault valueDescription
configObjectNoThe object passed in when constructing the urlURL configuration object.

config object field

FieldTypeRequiredDefault valueDescription
schemeStringNohttpURL scheme, no need to write ://.
hostStringNoEmpty stringHostname or IP. When there is no host, the URL starting with scheme:// will still be generated according to the current implementation.
portInt/StringNoNonePort number. If the field value is not empty or null, it will be converted to a string and spelled after host.
pathStringNo/URL path. If it does not start with /, / will be automatically added.
queryObject/String/AnyNoNoneQuery string. Object will be URL encoded item by key value; String will be used as query string as it is; other values will be converted to string first. Empty strings are not written to ?.
fragmentStringNoNoneURL fragment, no need to write #; percent encoding will be done when writing.
usernameStringNoNoneUsername. If it is not empty, username@ will be written and percent-encoded.
passwordStringNoNonePassword. Only when username is non-empty, it will be written and percent-encoded.

Return value

TypeDescription
StringThe constructed URL.

Code Example

Notes

  • When query is Object, field names and values will be encoded according to URL component rules.
  • build() only does lightweight string construction and does not check whether host is empty or whether scheme actually exists.