query_params.go 716 B

123456789101112131415161718192021222324252627282930313233343536
  1. package http
  2. import (
  3. "net/http"
  4. "strings"
  5. )
  6. func NewLowerQueryParamsHandler(hdlr http.Handler) *LowerQueryParamsHandler {
  7. return &LowerQueryParamsHandler{hdlr}
  8. }
  9. type LowerQueryParamsHandler struct {
  10. Handler http.Handler
  11. }
  12. func (h *LowerQueryParamsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
  13. err := req.ParseForm()
  14. if err == nil {
  15. lowerBoolQueryParams(req)
  16. }
  17. h.Handler.ServeHTTP(w, req)
  18. }
  19. func lowerBoolQueryParams(req *http.Request) {
  20. form := req.Form
  21. for key, vals := range form {
  22. for i, val := range vals {
  23. lowered := strings.ToLower(val)
  24. if lowered == "true" || lowered == "false" {
  25. req.Form[key][i] = lowered
  26. } else {
  27. req.Form[key][i] = val
  28. }
  29. }
  30. }
  31. }