mirror of https://github.com/golang-jwt/jwt.git
Refactor example: use io.ReadAll instead of io.Copy (#320)
This commit is contained in:
parent
b2b650971a
commit
f53600aa9f
|
@ -4,7 +4,6 @@ package jwt_test
|
|||
// This is based on a (now outdated) example at https://gist.github.com/cryptix/45c33ecf0ae54828e63b
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rsa"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -93,11 +92,10 @@ func Example_getTokenViaHTTP() {
|
|||
}
|
||||
|
||||
// Read the token out of the response body
|
||||
buf := new(bytes.Buffer)
|
||||
_, err = io.Copy(buf, res.Body)
|
||||
buf, err := io.ReadAll(res.Body)
|
||||
fatal(err)
|
||||
res.Body.Close()
|
||||
tokenString := strings.TrimSpace(buf.String())
|
||||
tokenString := strings.TrimSpace(string(buf))
|
||||
|
||||
// Parse the token
|
||||
token, err := jwt.ParseWithClaims(tokenString, &CustomClaimsExample{}, func(token *jwt.Token) (interface{}, error) {
|
||||
|
@ -128,11 +126,10 @@ func Example_useTokenViaHTTP() {
|
|||
fatal(err)
|
||||
|
||||
// Read the response body
|
||||
buf := new(bytes.Buffer)
|
||||
_, err = io.Copy(buf, res.Body)
|
||||
buf, err := io.ReadAll(res.Body)
|
||||
fatal(err)
|
||||
res.Body.Close()
|
||||
fmt.Println(buf.String())
|
||||
fmt.Printf("%s", buf)
|
||||
|
||||
// Output: Welcome, foo
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue