HTTP Servers
Response Model
A response combines a status code, body text, and headers.
response
Server code fills a response value so the caller can inspect status, body, and headers.
Response Model
response_model.go
Replay: real traced execution (multi-file project)
package main
import "fmt"
type response struct {
Status int
Body string
Headers map[string]string
}
func main() {
var userID = "42"
res := response{Status: 200, Body: "user " + userID, Headers: map[string]string{}}
if userID == "missing" {
res.Status = 404
res.Body = "user not found"
}
res.Headers["Content-Type"] = "text/plain"
fmt.Println("userID=", userID)
fmt.Println("status=", res.Status)
fmt.Println("body=", res.Body)
fmt.Println("contentType=", res.Headers["Content-Type"])
}
package main
import "fmt"
type response struct {
Status int
Body string
Headers map[string]string
}
func main() {
var userID = "7"
res := response{Status: 200, Body: "user " + userID, Headers: map[string]string{}}
if userID == "missing" {
res.Status = 404
res.Body = "user not found"
}
res.Headers["Content-Type"] = "text/plain"
fmt.Println("userID=", userID)
fmt.Println("status=", res.Status)
fmt.Println("body=", res.Body)
fmt.Println("contentType=", res.Headers["Content-Type"])
}
package main
import "fmt"
type response struct {
Status int
Body string
Headers map[string]string
}
func main() {
var userID = "missing"
res := response{Status: 200, Body: "user " + userID, Headers: map[string]string{}}
if userID == "missing" {
res.Status = 404
res.Body = "user not found"
}
res.Headers["Content-Type"] = "text/plain"
fmt.Println("userID=", userID)
fmt.Println("status=", res.Status)
fmt.Println("body=", res.Body)
fmt.Println("contentType=", res.Headers["Content-Type"])
}
userID ← "42", res ← main.response{Status:200, Body:"user 42", Headers:map[string]string{}}
11func main() {12 var userID→ "42" = "42" //@userID="7", "missing"13 res→ main.response{Status:200, Body:"user 42", Headers:map[string]string{}} := response{Status: 200, Body: "user " + userID"42", Headers: map[string]string{}}1415 if userID == "missing" {16 res.Status = 40417 res.Body = "user not found"18 }19 res.Headers["Content-Type"]→ "text/plain" = "text/plain"2021 fmt.Println("userID=", userID"42")22 fmt.Println("status=", res.Status200)23 fmt.Println("body=", res.Body"user 42")24 fmt.Println("contentType=", res.Headers["Content-Type"]"text/plain")25}outputuserID= 42 status= 200 body= user 42 contentType= text/plain
userID ← "7", res ← main.response{Status:200, Body:"user 7", Headers:map[string]string{}}
11func main() {12 var userID→ "7" = "7"13 res→ main.response{Status:200, Body:"user 7", Headers:map[string]string{}} := response{Status: 200, Body: "user " + userID"7", Headers: map[string]string{}}1415 if userID == "missing" {16 res.Status = 40417 res.Body = "user not found"18 }19 res.Headers["Content-Type"]→ "text/plain" = "text/plain"2021 fmt.Println("userID=", userID"7")22 fmt.Println("status=", res.Status200)23 fmt.Println("body=", res.Body"user 7")24 fmt.Println("contentType=", res.Headers["Content-Type"]"text/plain")25}outputuserID= 7 status= 200 body= user 7 contentType= text/plain
userID ← "missing", res ← main.response{Status:200, Body:"user missing", Headers:map[string]string{}}
11func main() {12 var userID→ "missing" = "missing"13 res→ main.response{Status:200, Body:"user missing", Headers:map[string]string{}} := response{Status: 200, Body: "user " + userID"missing", Headers: map[string]string{}}res.Status ← 404, res.Body ← "user not found"
15if userID"missing" == "missing" {16 res.Status→ 404 = 40417 res.Body→ "user not found" = "user not found"18}res.Headers["Content-Type"] ← "text/plain"
18 }19 res.Headers["Content-Type"]→ "text/plain" = "text/plain"2021 fmt.Println("userID=", userID"missing")22 fmt.Println("status=", res.Status404)23 fmt.Println("body=", res.Body"user not found")24 fmt.Println("contentType=", res.Headers["Content-Type"]"text/plain")25}outputuserID= missing status= 404 body= user not found contentType= text/plain