Get the HTTP Method from a Request · GolangCode
阿新 • • 發佈:2018-12-29
When working with any form of http communication in Go you’re going to come across scenarios where you need to know a request’s method - whether it’s a GET
, POST
, etc.
If you’re writing an API with Go you’ll most likely have the incoming request in the form of a http.Request
object. This request has Method
package main import ( "net/http" "io" ) func main() { // Create a basic http example to demonstrate example http.Handle("/", http.HandlerFunc(ExampleHandler)) http.ListenAndServe(":8080", nil) } func ExampleHandler(w http.ResponseWriter, r *http.Request) { // This handler will run for all types of HTTP request, but we can use r.Method to // determine which method is being used and validate the request based on this. if r.Method == http.MethodGet { io.WriteString(w, "This is a get request") } else if r.Method == http.MethodPost { io.WriteString(w, "This is a post request") } else { io.WriteString(w, "This is a " + r.Method + " request") } }