response.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package xhttp
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "net/http"
  6. "net/url"
  7. )
  8. // Message HTTP response
  9. type Message struct {
  10. req *http.Request
  11. res *http.Response
  12. StatusCode int
  13. Body []byte
  14. Header http.Header
  15. }
  16. // JSON Body to JSON
  17. func (m Message) JSON(v interface{}) error {
  18. return json.Unmarshal(m.Body, v)
  19. }
  20. // XML Body to XML
  21. func (m Message) XML(v interface{}) error {
  22. return xml.Unmarshal(m.Body, v)
  23. }
  24. // Cookies parses and returns the cookies set in the Set-Cookie headers.
  25. func (m Message) Cookies() []*http.Cookie {
  26. return m.res.Cookies()
  27. }
  28. // Location returns the URL of the response's "Location" header, if present. Relative redirects are resolved relative to the Response's Request. ErrNoLocation is returned if no Location header is present.
  29. func (m Message) Location() (*url.URL, error) {
  30. return m.res.Location()
  31. }
  32. // UserAgent returns the client's User-Agent, if sent in the request.
  33. func (m Message) UserAgent() string {
  34. return m.req.UserAgent()
  35. }
  36. // Referer returns the referring URL, if sent in the request.
  37. func (m Message) Referer() string {
  38. return m.req.Referer()
  39. }