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