http.go 900 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package utils
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "io/ioutil"
  6. "net/http"
  7. )
  8. /**
  9. Params:
  10. argUrl: reqeust url
  11. argReq: reqeust contents
  12. argType: reqeust type
  13. argHead: reqeust head
  14. Retrun: reqesut result body
  15. */
  16. func SendHttpRequest(argUrl string, argReq string, argType string, argHead map[string]string) ([]byte, error) {
  17. bReq := []byte(argReq)
  18. req, err := http.NewRequest(argType, argUrl, bytes.NewBuffer(bReq))
  19. if err != nil {
  20. return []byte{}, err
  21. }
  22. req.Header.Set("Content-Type", "application/json")
  23. if argHead != nil {
  24. for key, vaule := range argHead {
  25. req.Header.Set(key, vaule)
  26. }
  27. }
  28. tr := &http.Transport{
  29. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  30. }
  31. client := &http.Client{Transport: tr}
  32. resp, err := client.Do(req)
  33. if err != nil {
  34. return []byte{}, err
  35. }
  36. defer resp.Body.Close()
  37. body, _ := ioutil.ReadAll(resp.Body)
  38. return body, nil
  39. }