Skip to content

Instantly share code, notes, and snippets.

@franchb
Created February 20, 2018 11:17
Show Gist options
  • Save franchb/d38fd9271e225a105a26c6859df1ce9b to your computer and use it in GitHub Desktop.
Save franchb/d38fd9271e225a105a26c6859df1ce9b to your computer and use it in GitHub Desktop.
reuse response Body in Golang
// read the response body to a variable
bodyBytes, _ := ioutil.ReadAll(response.Body)
// Use io.Copy to just dump the response body to the file. This supports huge files
err := ioutil.WriteFile("tmp/asdf.png", bodyBytes, 0644)
if err != nil {
log.Fatal(err)
}
fmt.Println("Captcha image saving success!")
//reset the response body to the original unread state
response.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
@BruceDone
Copy link

it costs too much memory when the response body is a huge file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment