Created
September 14, 2019 17:29
-
-
Save rianby64/d09d67d1a090a708d7668791e3efc863 to your computer and use it in GitHub Desktop.
Reader for io.ReadCloser
This file contains 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
// Nice reader for request.Body | |
func readStream(stream io.ReadCloser) (string, error) { | |
defer stream.Close() | |
body := "" | |
buffer := make([]byte, 4) | |
for { | |
n, err := stream.Read(buffer) | |
if err != nil { | |
if err == io.EOF { | |
body = body + string(buffer[:n]) | |
break | |
} | |
return "", err | |
} | |
body = body + string(buffer) | |
} | |
return body, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment