request_1_7.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // +build !go1.8
  2. package request
  3. import "io"
  4. // NoBody is an io.ReadCloser with no bytes. Read always returns EOF
  5. // and Close always returns nil. It can be used in an outgoing client
  6. // request to explicitly signal that a request has zero bytes.
  7. // An alternative, however, is to simply set Request.Body to nil.
  8. //
  9. // Copy of Go 1.8 NoBody type from net/http/http.go
  10. type noBody struct{}
  11. func (noBody) Read([]byte) (int, error) { return 0, io.EOF }
  12. func (noBody) Close() error { return nil }
  13. func (noBody) WriteTo(io.Writer) (int64, error) { return 0, nil }
  14. // NoBody is an empty reader that will trigger the Go HTTP client to not include
  15. // and body in the HTTP request.
  16. var NoBody = noBody{}
  17. // ResetBody rewinds the request body back to its starting position, and
  18. // sets the HTTP Request body reference. When the body is read prior
  19. // to being sent in the HTTP request it will need to be rewound.
  20. //
  21. // ResetBody will automatically be called by the SDK's build handler, but if
  22. // the request is being used directly ResetBody must be called before the request
  23. // is Sent. SetStringBody, SetBufferBody, and SetReaderBody will automatically
  24. // call ResetBody.
  25. func (r *Request) ResetBody() {
  26. body, err := r.getNextRequestBody()
  27. if err != nil {
  28. r.Error = err
  29. return
  30. }
  31. r.HTTPRequest.Body = body
  32. }