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)));
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
Output
http:
bob:secret
8080
sub.example.com
/somepath
{ foo: 'bar' }
1
2
3
4
5
6
2
3
4
5
6
# 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())
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Output
http
bob:secret
8080
sub.example.com
/somepath
map[foo:[bar]]
1
2
3
4
5
6
2
3
4
5
6
编辑 (opens new window)
上次更新: 2022/09/30, 11:34:22