MCPcopy Create free account
hub / github.com/golang-jwt/jwt / Example_useTokenViaHTTP

Function Example_useTokenViaHTTP

http_example_test.go:115–137  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

113}
114
115func Example_useTokenViaHTTP() {
116
117 // Make a sample token
118 // In a real world situation, this token will have been acquired from
119 // some other API call (see Example_getTokenViaHTTP)
120 token, err := createToken("foo")
121 fatal(err)
122
123 // Make request. See func restrictedHandler for example request processor
124 req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%v/restricted", serverPort), nil)
125 fatal(err)
126 req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token))
127 res, err := http.DefaultClient.Do(req)
128 fatal(err)
129
130 // Read the response body
131 buf := new(bytes.Buffer)
132 io.Copy(buf, res.Body)
133 res.Body.Close()
134 fmt.Println(buf.String())
135
136 // Output: Welcome, foo
137}
138
139func createToken(user string) (string, error) {
140 // create a signer for rsa 256

Callers

nothing calls this directly

Calls 4

createTokenFunction · 0.85
fatalFunction · 0.85
SetMethod · 0.80
StringMethod · 0.80

Tested by

no test coverage detected