response.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "encoding/json"
  7. "io/ioutil"
  8. "net/http"
  9. )
  10. // Response represents a response from Elasticsearch.
  11. type Response struct {
  12. // StatusCode is the HTTP status code, e.g. 200.
  13. StatusCode int
  14. // Header is the HTTP header from the HTTP response.
  15. // Keys in the map are canonicalized (see http.CanonicalHeaderKey).
  16. Header http.Header
  17. // Body is the deserialized response body.
  18. Body json.RawMessage
  19. }
  20. // newResponse creates a new response from the HTTP response.
  21. func (c *Client) newResponse(res *http.Response) (*Response, error) {
  22. r := &Response{
  23. StatusCode: res.StatusCode,
  24. Header: res.Header,
  25. }
  26. if res.Body != nil {
  27. slurp, err := ioutil.ReadAll(res.Body)
  28. if err != nil {
  29. return nil, err
  30. }
  31. // HEAD requests return a body but no content
  32. if len(slurp) > 0 {
  33. r.Body = json.RawMessage(slurp)
  34. }
  35. }
  36. return r, nil
  37. }