MCPcopy
hub / github.com/golang-jwt/jwt / Example_useTokenViaHTTP

Function Example_useTokenViaHTTP

http_example_test.go:112–133  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

createTokenFunction · 0.85
fatalFunction · 0.85
SetMethod · 0.80

Tested by

no test coverage detected