Skip to content

url_parse() parses a URL into its component pieces; url_build() does the reverse, converting a list of pieces into a string URL. See RFC 3986 for the details of the parsing algorithm.

Usage

url_parse(url)

url_build(url)

Arguments

url

For url_parse() a string to parse into a URL; for url_build() a URL to turn back into a string.

Value

  • url_build() returns a string.

  • url_parse() returns a URL: a S3 list with class httr2_url and elements scheme, hostname, port, path, fragment, query, username, password.

Examples

url_parse("http://google.com/")
#> <httr2_url> http://google.com/
#>scheme: http
#>hostname: google.com
#>path: /
url_parse("http://google.com:80/")
#> <httr2_url> http://google.com:80/
#>scheme: http
#>hostname: google.com
#>port: 80
#>path: /
url_parse("http://google.com:80/?a=1&b=2")
#> <httr2_url> http://google.com:80/?a=1&b=2
#>scheme: http
#>hostname: google.com
#>port: 80
#>path: /
#>query:
#>a: 1
#>b: 2
url_parse("http://username@google.com:80/path;test?a=1&b=2#40")
#> <httr2_url> http://username@google.com:80/path;test?a=1&b=2#40
#>scheme: http
#>hostname: google.com
#>username: username
#>port: 80
#>path: /path;test
#>query:
#>a: 1
#>b: 2
#>fragment: 40

url <- url_parse("http://google.com/")
url$port <- 80
url$hostname <- "example.com"
url$query <- list(a = 1, b = 2, c = 3)
url_build(url)
#> [1] "http://example.com:80/?a=1&b=2&c=3"