Created
February 20, 2018 11:17
-
-
Save franchb/d38fd9271e225a105a26c6859df1ce9b to your computer and use it in GitHub Desktop.
reuse response Body in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it costs too much memory when the response body is a huge file