Skip to content

Commit 3ac45aa

Browse files
committed
README: Add example outputs as comments
1 parent 490dfd6 commit 3ac45aa

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,30 @@ If you're system doesn't allow installing `cryptography` like on Google App Engi
2828

2929
```python
3030
import jwt
31-
jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256')
31+
encoded = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256')
32+
# 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.4twFt5NiznN84AWoo1d7KO1T_yoc0Z6XOpOVswacPZg'
3233
```
3334

3435
Additional headers may also be specified.
3536

3637
```python
3738
jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256', headers={'kid': '230498151c214b788dd97f22b85410a5'})
39+
# 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjIzMDQ5ODE1MWMyMTRiNzg4ZGQ5N2YyMmI4NTQxMGE1In0.eyJzb21lIjoicGF5bG9hZCJ9.DogbDGmMHgA_bU05TAB-R6geQ2nMU2BRM-LnYEtefwg'
3840
```
3941

4042
Note the resulting JWT will not be encrypted, but verifiable with a secret key.
4143

4244
```python
43-
jwt.decode('someJWTstring', 'secret', algorithms=['HS256'])
45+
jwt.decode(encoded, 'secret', algorithms=['HS256'])
46+
# {u'some': u'payload'}
4447
```
4548

4649
If the secret is wrong, it will raise a `jwt.DecodeError` telling you as such.
4750
You can still get the payload by setting the `verify` argument to `False`.
4851

4952
```python
50-
jwt.decode('someJWTstring', verify=False)
53+
jwt.decode(encoded, verify=False)
54+
# {u'some': u'payload'}
5155
```
5256

5357
The `decode()` function can raise other exceptions, e.g. for invalid issuer or
@@ -57,7 +61,7 @@ use this approach to catch any issues relating to invalid tokens:
5761

5862
```python
5963
try:
60-
payload = jwt.decode('someJWTstring')
64+
payload = jwt.decode(encoded)
6165
except jwt.InvalidTokenError:
6266
pass # do something sensible here, e.g. return HTTP 403 status code
6367
```
@@ -83,7 +87,8 @@ options = {
8387
'verify_exp': True,
8488
}
8589

86-
jwt.decode('someJWTstring', 'secret', options=options)
90+
jwt.decode(encoded, 'secret', options=options)
91+
# {u'some': u'payload'}
8792
```
8893

8994
**NOTE**: *Changing the default behavior is done at your own risk, and almost certainly will make your
@@ -121,7 +126,8 @@ You can specify which algorithm you would like to use to sign the JWT
121126
by using the `algorithm` parameter:
122127

123128
```python
124-
jwt.encode({'some': 'payload'}, 'secret', algorithm='HS512')
129+
encoded = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS512')
130+
# 'eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.WTzLzFO079PduJiFIyzrOah54YaM8qoxH9fLMQoQhKtw3_fMGjImIOokijDkXVbyfBqhMo2GCNu4w9v7UXvnpA'
125131
```
126132

127133
### Decoding
@@ -130,7 +136,8 @@ when validating the JWT by using the `algorithms` parameter which takes a list
130136
of allowed algorithms:
131137

132138
```python
133-
jwt.decode(some_jwt, 'secret', algorithms=['HS512', 'HS256'])
139+
jwt.decode(encoded, 'secret', algorithms=['HS512', 'HS256'])
140+
# {u'some': u'payload'}
134141
```
135142

136143
In the above case, if the JWT has any value for its alg header other than

0 commit comments

Comments
 (0)