Açıklama Yok

request.go 802B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package request
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. )
  8. //Post : retorna um parametro post
  9. func Post(r *http.Request, parameter string) string {
  10. if err := r.ParseForm(); err != nil {
  11. fmt.Printf("ParseForm() err: %v", err)
  12. return ""
  13. }
  14. return r.FormValue(parameter)
  15. }
  16. //Get : retorna um parametro get
  17. func Get(r *http.Request, parameter string) string {
  18. keys, ok := r.URL.Query()[parameter]
  19. if !ok || len(keys[0]) < 1 {
  20. log.Println("Url Param 'key' is missing")
  21. return ""
  22. }
  23. key := keys[0]
  24. return string(key)
  25. }
  26. func SetContextValue(r *http.Request, k string, v interface{}) *http.Request {
  27. ctx := context.WithValue(r.Context(), k, v)
  28. return r.WithContext(ctx)
  29. }
  30. func GetContextValue(r *http.Request, parameter string) interface{} {
  31. return r.Context().Value(parameter)
  32. }