WriteTo reads the response and then copies its data to the "dest" writer. If the "dest" is a type of HTTP response writer then it writes the content-type and content-length of the original request. Returns the amount of bytes written to "dest".
(ctx context.Context, dest io.Writer, method, urlpath string, payload interface{}, opts ...RequestOption)
| 480 | // |
| 481 | // Returns the amount of bytes written to "dest". |
| 482 | func (c *Client) WriteTo(ctx context.Context, dest io.Writer, method, urlpath string, payload interface{}, opts ...RequestOption) (int64, error) { |
| 483 | if payload != nil { |
| 484 | opts = append(opts, RequestHeader(true, contentTypeKey, contentTypeJSON)) |
| 485 | } |
| 486 | |
| 487 | resp, err := c.Do(ctx, method, urlpath, payload, opts...) |
| 488 | if err != nil { |
| 489 | return 0, err |
| 490 | } |
| 491 | defer resp.Body.Close() |
| 492 | |
| 493 | if w, ok := dest.(http.ResponseWriter); ok { |
| 494 | // Copy the content type and content-length. |
| 495 | w.Header().Set("Content-Type", resp.Header.Get("Content-Type")) |
| 496 | if resp.ContentLength > 0 { |
| 497 | w.Header().Set("Content-Length", strconv.FormatInt(resp.ContentLength, 10)) |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | return io.Copy(dest, resp.Body) |
| 502 | } |
| 503 | |
| 504 | // BindResponse consumes the response's body and binds the result to the "dest" pointer, |
| 505 | // closing the response's body is up to the caller. |