Files

26 lines
746 B
Go
Raw Permalink Normal View History

package httputil
import (
"net/http"
)
// RequireMethod checks if the request method matches expected.
// Returns true if method matches, false otherwise (sends 405 error).
func RequireMethod(w http.ResponseWriter, r *http.Request, method string) bool {
if r.Method != method {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return false
}
return true
}
// RequirePost is a shorthand for RequireMethod(w, r, http.MethodPost).
func RequirePost(w http.ResponseWriter, r *http.Request) bool {
return RequireMethod(w, r, http.MethodPost)
}
// RequireGet is a shorthand for RequireMethod(w, r, http.MethodGet).
func RequireGet(w http.ResponseWriter, r *http.Request) bool {
return RequireMethod(w, r, http.MethodGet)
}