1
0
Fork 0

Read, and close, response body ASAP

master
Philip O'Toole 5 years ago
parent c69ef51790
commit 9ab39b7b29

@ -206,13 +206,16 @@ func sendRequest(ctx *cli.Context, makeNewRequest func(string) (*http.Request, e
if err != nil {
return nil, err
}
defer resp.Body.Close()
response, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body.Close()
if resp.StatusCode == http.StatusUnauthorized {
return nil, fmt.Errorf("unauthorized")
}
// Check for redirect.
if resp.StatusCode == http.StatusMovedPermanently {
nRedirect++
if nRedirect > maxRedirect {
@ -222,16 +225,10 @@ func sendRequest(ctx *cli.Context, makeNewRequest func(string) (*http.Request, e
continue
}
// Check for successful response
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("server responded with: %s", resp.Status)
}
response, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return &response, nil
}
}

Loading…
Cancel
Save