python.requests.best-practice.use-response-json-shortcut.python.requests.best-practice.use-response-json-shortcut

Author
1,938
Download Count*
License
The requests library has a convenient shortcut for reading JSON responses, which lets you stop worrying about deserializing the response yourself.
Run Locally
Run in CI
Defintion
rules:
- id: python.requests.best-practice.use-response-json-shortcut
patterns:
- pattern-inside: import json; ...
- pattern-inside: import requests; ...
- pattern-inside: $RESP = requests.$METHOD(...); ...
- pattern: json.loads($RESP.text)
fix: $RESP.json()
message: The requests library has a convenient shortcut for reading JSON
responses, which lets you stop worrying about deserializing the response
yourself.
severity: WARNING
metadata:
references:
- https://requests.readthedocs.io/en/stable/user/quickstart/#json-response-content
category: best-practice
technology:
- requests
license: Commons Clause License Condition v1.0[LGPL-2.1-only]
languages:
- python
Examples
use-response-json-shortcut.py
import json
import requests
r = requests.get("https://example.org")
# ok: python.requests.best-practice.use-response-json-shortcut
payload = r.json()
# ruleid: python.requests.best-practice.use-response-json-shortcut
payload = json.loads(r.text)
Short Link: https://sg.run/GW2p