Hug
raw JSON → 2.6.1 verified Mon Apr 27 auth: no python maintenance
Hug is a Python 3+ framework for developing REST APIs as simply as possible. It supports both synchronous and asynchronous endpoints, automatic input validation, and built-in interactive documentation. Current version is 2.6.1 (released August 2019). The project is in maintenance mode (no releases since 2019).
Common errors
error TypeError: 'NoneType' object is not callable ↓
cause Using `hug.API(__name__).http.serve()` after upgrading Falcon without pinning.
fix
Pin falcon<2.0 or set
__hug__.future = True before importing hug. error ImportError: cannot import name 'route' from 'hug' ↓
cause Incorrect import path. 'route' is not a submodule.
fix
Use
import hug and then @hug.route('/path') or from hug import route (direct import from hug package). error TypeError: a bytes-like object is required, not 'str' ↓
cause Using Python 3 with Falcon < 2.0 but passing non-bytes body.
fix
Ensure your response handlers return strings or use
hug.output_format.json properly. Warnings
breaking Falcon 2.0+ changes broke hug. Hug forces Falcon <2.0 by default. If you need newer Falcon, set `__hug__.future = True` before importing hug endpoints, but expect breakage. ↓
fix Pin falcon <2.0 or test with `__hug__.future = True`
deprecated Python 3.5 support dropped as of 2.5.1. ↓
fix Use Python >=3.6
gotcha POST requests broke in 2.5.0 (fixed in 2.5.1). Ensure you are on at least 2.5.1 if using POST. ↓
fix Upgrade to hug >=2.5.1
gotcha Empty query params are not ignored on 2.5.0 (fixed in 2.5.1). ↓
fix Upgrade to hug >=2.5.1
Imports
- API
from hug import API - hug.get/post/put/delete
import hug @hug.get('/path') - route decorator
from hug import route
Quickstart
import hug
@hug.get('/hello')
def hello(name: str):
return {'hello': name}
if __name__ == '__main__':
hug.API(__name__).http.serve()