HTTP Clients
Build Request
An HTTP request value stores the method, URL, host, and path before any network call is made.
request object
A client prepares a method, host, and path before any network call is made.
Build Request
build_request.go
Replay: real traced execution (multi-file project)
package main
import "fmt"
func splitEndpoint(endpoint string) (string, string) {
rest := endpoint[len("https://"):]
slashIndex := 0
for index, letter := range rest {
if letter == '/' {
slashIndex = index
break
}
}
host := rest[:slashIndex]
path := rest[slashIndex:]
return host, path
}
func main() {
var endpoint = "https://api.example.test/users"
host, path := splitEndpoint(endpoint)
method := "GET"
fmt.Println("method=", method)
fmt.Println("host=", host)
fmt.Println("path=", path)
}
package main
import "fmt"
func splitEndpoint(endpoint string) (string, string) {
rest := endpoint[len("https://"):]
slashIndex := 0
for index, letter := range rest {
if letter == '/' {
slashIndex = index
break
}
}
host := rest[:slashIndex]
path := rest[slashIndex:]
return host, path
}
func main() {
var endpoint = "https://api.example.test/projects"
host, path := splitEndpoint(endpoint)
method := "GET"
fmt.Println("method=", method)
fmt.Println("host=", host)
fmt.Println("path=", path)
}
package main
import "fmt"
func splitEndpoint(endpoint string) (string, string) {
rest := endpoint[len("https://"):]
slashIndex := 0
for index, letter := range rest {
if letter == '/' {
slashIndex = index
break
}
}
host := rest[:slashIndex]
path := rest[slashIndex:]
return host, path
}
func main() {
var endpoint = "https://docs.example.test/search"
host, path := splitEndpoint(endpoint)
method := "GET"
fmt.Println("method=", method)
fmt.Println("host=", host)
fmt.Println("path=", path)
}
endpoint ← "https://api.example.test/users"
19func main() {20 var endpoint→ "https://api.example.test/users" = "https://api.example.test/users" //@endpoint="https://api.example.test/projects", "https://docs.example.test/search"21 host, path := splitEndpoint(endpoint"https://api.example.test/users")rest ← "api.example.test/users", slashIndex ← 0
5func splitEndpoint(endpoint"https://api.example.test/users" string) (string, string) {6 rest→ "api.example.test/users" := endpoint"https://api.example.test/users"[len("https://"):]7 slashIndex→ 0 := 08 for index, letter := range rest {for index, letter := range rest
pass 1 of 177slashIndex := 08for index0, letter97 := range rest"api.example.test/users" {9 if letter == '/' {17 passes — pass 1 is the card above pass indexletterslashIndex1 0 97 — 2 1 112 — 3 2 105 — 4 3 46 — 5 4 101 — 6 5 120 — 7 6 97 — 8 7 109 — 9 8 112 — ⋯ 6 more passes ⋯ 16 15 116 — 17 16 47 0 → 16 slashIndex ← 16
8for index, letter := range rest {9 if letter47 == '/' {10 slashIndex→ 16 = index1611 break12 }host ← "api.example.test"
13 }14 host→ "api.example.test" := rest"api.example.test/users"[:slashIndex16]15 path := rest"api.example.test/users"[slashIndex16:]16 return host"api.example.test", path17}host ← "api.example.test", method ← "GET"
20 var endpoint = "https://api.example.test/users" //@endpoint="https://api.example.test/projects", "https://docs.example.test/search"21 host→ "api.example.test", path := splitEndpoint(endpoint"https://api.example.test/users")2223 method→ "GET" := "GET"24 fmt.Println("method=", method"GET")25 fmt.Println("host=", host"api.example.test")26 fmt.Println("path=", path)27}outputmethod= GET host= api.example.test path= /users
endpoint ← "https://api.example.test/projects"
19func main() {20 var endpoint→ "https://api.example.test/projects" = "https://api.example.test/projects"21 host, path := splitEndpoint(endpoint"https://api.example.test/projects")rest ← "api.example.test/projects", slashIndex ← 0
5func splitEndpoint(endpoint"https://api.example.test/projects" string) (string, string) {6 rest→ "api.example.test/projects" := endpoint"https://api.example.test/projects"[len("https://"):]7 slashIndex→ 0 := 08 for index, letter := range rest {for index, letter := range rest
pass 1 of 177slashIndex := 08for index0, letter97 := range rest"api.example.test/projects" {9 if letter == '/' {17 passes — pass 1 is the card above pass indexletterslashIndex1 0 97 — 2 1 112 — 3 2 105 — 4 3 46 — 5 4 101 — 6 5 120 — 7 6 97 — 8 7 109 — 9 8 112 — ⋯ 6 more passes ⋯ 16 15 116 — 17 16 47 0 → 16 slashIndex ← 16
8for index, letter := range rest {9 if letter47 == '/' {10 slashIndex→ 16 = index1611 break12 }host ← "api.example.test"
13 }14 host→ "api.example.test" := rest"api.example.test/projects"[:slashIndex16]15 path := rest"api.example.test/projects"[slashIndex16:]16 return host"api.example.test", path17}host ← "api.example.test", method ← "GET"
20 var endpoint = "https://api.example.test/projects"21 host→ "api.example.test", path := splitEndpoint(endpoint"https://api.example.test/projects")2223 method→ "GET" := "GET"24 fmt.Println("method=", method"GET")25 fmt.Println("host=", host"api.example.test")26 fmt.Println("path=", path)27}outputmethod= GET host= api.example.test path= /projects
endpoint ← "https://docs.example.test/search"
19func main() {20 var endpoint→ "https://docs.example.test/search" = "https://docs.example.test/search"21 host, path := splitEndpoint(endpoint"https://docs.example.test/search")rest ← "docs.example.test/search", slashIndex ← 0
5func splitEndpoint(endpoint"https://docs.example.test/search" string) (string, string) {6 rest→ "docs.example.test/search" := endpoint"https://docs.example.test/search"[len("https://"):]7 slashIndex→ 0 := 08 for index, letter := range rest {for index, letter := range rest
pass 1 of 187slashIndex := 08for index0, letter100 := range rest"docs.example.test/search" {9 if letter == '/' {18 passes — pass 1 is the card above pass indexletterslashIndex1 0 100 — 2 1 111 — 3 2 99 — 4 3 115 — 5 4 46 — 6 5 101 — 7 6 120 — 8 7 97 — 9 8 109 — ⋯ 7 more passes ⋯ 17 16 116 — 18 17 47 0 → 17 slashIndex ← 17
8for index, letter := range rest {9 if letter47 == '/' {10 slashIndex→ 17 = index1711 break12 }host ← "docs.example.test"
13 }14 host→ "docs.example.test" := rest"docs.example.test/search"[:slashIndex17]15 path := rest"docs.example.test/search"[slashIndex17:]16 return host"docs.example.test", path17}host ← "docs.example.test", method ← "GET"
20 var endpoint = "https://docs.example.test/search"21 host→ "docs.example.test", path := splitEndpoint(endpoint"https://docs.example.test/search")2223 method→ "GET" := "GET"24 fmt.Println("method=", method"GET")25 fmt.Println("host=", host"docs.example.test")26 fmt.Println("path=", path)27}outputmethod= GET host= docs.example.test path= /search