HTTP Servers
Response Model
A response combines a status code, body text, and headers.
Response Model
response_model.go
package main
import "fmt"
type response struct {
Status int
Body string
Headers map[string]string
}
func main() {
var userID =
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 =
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 =
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"])
}
response
Server code fills a response value so the caller can inspect status, body, and headers.