HTTP Servers
Method Checks
Handlers often accept one HTTP method and reject others.
Method Checks
method_checks.go
package main
import "fmt"
type serverRequest struct {
Method string
Path string
}
func main() {
var method =
req := serverRequest{Method: method, Path: "/items"}
status := 200
body := "list items"
if req.Method == "POST" {
status = 201
body = "created item"
}
if req.Method != "GET" && req.Method != "POST" {
status = 405
body = "method not allowed"
}
fmt.Println("method=", req.Method)
fmt.Println("status=", status)
fmt.Println("body=", body)
}
package main
import "fmt"
type serverRequest struct {
Method string
Path string
}
func main() {
var method =
req := serverRequest{Method: method, Path: "/items"}
status := 200
body := "list items"
if req.Method == "POST" {
status = 201
body = "created item"
}
if req.Method != "GET" && req.Method != "POST" {
status = 405
body = "method not allowed"
}
fmt.Println("method=", req.Method)
fmt.Println("status=", status)
fmt.Println("body=", body)
}
package main
import "fmt"
type serverRequest struct {
Method string
Path string
}
func main() {
var method =
req := serverRequest{Method: method, Path: "/items"}
status := 200
body := "list items"
if req.Method == "POST" {
status = 201
body = "created item"
}
if req.Method != "GET" && req.Method != "POST" {
status = 405
body = "method not allowed"
}
fmt.Println("method=", req.Method)
fmt.Println("status=", status)
fmt.Println("body=", body)
}
method check
Checking a method keeps read and write actions separate.