url parse

url parse


Node.js

const url = require('url')
const qs = require('querystring')

const urlstr = 'http://bob:secret@sub.example.com:8080/somepath?foo=bar'

const parsed = url.parse(urlstr)
console.log(parsed.protocol)
console.log(parsed.auth)
console.log(parsed.port)
console.log(parsed.hostname)
console.log(parsed.pathname)
console.log(qs.parse(parsed.search.substr(1)))

Output

http:
bob:secret
8080
sub.example.com
/somepath
{ foo: 'bar' }

Go

package main

import (
    "fmt"
    "net/url"
)

func main() {
    urlstr := "http://bob:secret@sub.example.com:8080/somepath?foo=bar"

    u, err := url.Parse(urlstr)
    if err != nil {
        panic(err)
    }

    fmt.Println(u.Scheme)
    fmt.Println(u.User)
    fmt.Println(u.Port())
    fmt.Println(u.Hostname())
    fmt.Println(u.Path)
    fmt.Println(u.Query())
}

Output

http
bob:secret
8080
sub.example.com
/somepath
map[foo:[bar]]