Skip to content

Commit

Permalink
resp close while first reading
Browse files Browse the repository at this point in the history
  • Loading branch information
imroc committed Feb 6, 2018
1 parent 44cd006 commit 2a9647f
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (r *Resp) ToBytes() ([]byte, error) {
if r.respBody != nil {
return r.respBody, nil
}
defer r.resp.Body.Close()
respBody, err := ioutil.ReadAll(r.resp.Body)
if err != nil {
r.err = err
Expand Down Expand Up @@ -110,13 +111,15 @@ func (r *Resp) ToFile(name string) error {
return r.download(file)
}

defer r.resp.Body.Close()
_, err = io.Copy(file, r.resp.Body)
return err
}

func (r *Resp) download(file *os.File) error {
p := make([]byte, 1024)
b := r.resp.Body
defer b.Close()
total := r.resp.ContentLength
var current int64
var lastTime time.Time
Expand Down

0 comments on commit 2a9647f

Please sign in to comment.