httpx.go 601 B

123456789101112131415161718192021222324252627282930
  1. package httpx
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "time"
  8. )
  9. func SendPost(_serviceAddress string, _route string, _post interface{}) ([]byte, error) {
  10. url := "http://" + _serviceAddress + _route
  11. bytesData, err := json.Marshal(_post)
  12. if err != nil {
  13. return nil, err
  14. }
  15. client := &http.Client{Timeout: 20 * time.Second}
  16. resp, err := client.Post(url, "application/json", bytes.NewBuffer(bytesData))
  17. if err != nil {
  18. return nil, err
  19. }
  20. defer resp.Body.Close()
  21. respBytes, err := ioutil.ReadAll(resp.Body)
  22. if err != nil {
  23. return nil, err
  24. }
  25. return respBytes, nil
  26. }