http_util.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package utils
  2. import (
  3. "github.com/gogf/gf/frame/g"
  4. "github.com/gogf/gf/net/ghttp"
  5. "github.com/hashicorp/go-uuid"
  6. )
  7. var UHttp *HttpUtil
  8. func init() {
  9. UHttp = NewHttpUtil()
  10. }
  11. type HttpUtil struct {
  12. }
  13. func NewHttpUtil() *HttpUtil {
  14. return &HttpUtil{}
  15. }
  16. const (
  17. MIMEJSON = "application/json"
  18. MIMEHTML = "text/html"
  19. MIMEXML = "application/xml"
  20. MIMEXML2 = "text/xml"
  21. MIMEPlain = "text/plain"
  22. MIMEPOSTForm = "application/x-www-form-urlencoded"
  23. MIMEMultipartPOSTForm = "multipart/form-data"
  24. MIMEPROTOBUF = "application/x-protobuf"
  25. MIMEMSGPACK = "application/x-msgpack"
  26. MIMEMSGPACK2 = "application/msgpack"
  27. MIMEYAML = "application/x-yaml"
  28. )
  29. func (s *HttpUtil) HttpClient(contentType string, system string, serviceName string) *ghttp.Client {
  30. uid, _ := uuid.GenerateUUID()
  31. client := g.Client()
  32. client.SetHeader("Content-Type", contentType)
  33. client.SetHeader("requestId", uid)
  34. client.SetHeader("sourceSystem", system)
  35. client.SetHeader("serviceName", serviceName)
  36. return client
  37. }