http.go 871 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net/http"
  6. "github.com/nsqio/nsq/internal/version"
  7. )
  8. var httpclient *http.Client
  9. var userAgent string
  10. func init() {
  11. userAgent = fmt.Sprintf("nsq_to_http v%s", version.Binary)
  12. }
  13. func HTTPGet(endpoint string) (*http.Response, error) {
  14. req, err := http.NewRequest("GET", endpoint, nil)
  15. if err != nil {
  16. return nil, err
  17. }
  18. req.Header.Set("User-Agent", userAgent)
  19. for key, val := range validCustomHeaders {
  20. req.Header.Set(key, val)
  21. }
  22. return httpclient.Do(req)
  23. }
  24. func HTTPPost(endpoint string, body *bytes.Buffer) (*http.Response, error) {
  25. req, err := http.NewRequest("POST", endpoint, body)
  26. if err != nil {
  27. return nil, err
  28. }
  29. req.Header.Set("User-Agent", userAgent)
  30. req.Header.Set("Content-Type", *contentType)
  31. for key, val := range validCustomHeaders {
  32. req.Header.Set(key, val)
  33. }
  34. return httpclient.Do(req)
  35. }