A Python WSGI middleware that allows HTTP method override via form parameters or headers. This enables HTML forms to use HTTP methods other than GET and POST by providing a method override mechanism.
- π Override HTTP methods via form parameters (
_method
) or custom headers - π‘οΈ Security-focused: Only allows overrides from POST requests
- π― Configurable allowed methods and parameters
- π Comprehensive logging for debugging and monitoring
- π Compatible with any WSGI application (Flask). Coming soon Django, FastAPI and etc
- β‘ Zero dependencies - uses only Python standard library
Install using pip:
pip install method-override
Or using Poetry:
poetry add method-override
from method_override import MethodOverrideMiddleware
# Wrap your WSGI application
app = MethodOverrideMiddleware(your_wsgi_app)
from flask import Flask
from method_override import MethodOverrideMiddleware
app = Flask(__name__)
# Apply the middleware
app.wsgi_app = MethodOverrideMiddleware(app.wsgi_app)
@app.put('/users/<int:user_id>')
def edit_user(user_id):
return f"Updating user {user_id}"
@app.delete('/users/<int:user_id>')
def delete_user(user_id):
return f"Deleting user {user_id}"
<!-- HTML forms can now use PUT, PATCH, DELETE methods -->
<form method="POST" action="/users/123">
<input type="hidden" name="_method" value="PUT">
<input type="text" name="name" placeholder="User name">
<button type="submit">Update User</button>
</form>
<form method="POST" action="/users/123">
<input type="hidden" name="_method" value="DELETE">
<button type="submit">Delete User</button>
</form>
// You can also use the X-HTTP-Method-Override header
fetch('/users/123', {
method: 'POST',
headers: {
'X-HTTP-Method-Override': 'PUT',
'Content-Type': 'application/json'
},
body: JSON.stringify({ name: 'John Doe' })
});
from method_override import MethodOverrideMiddleware
app = MethodOverrideMiddleware(
your_wsgi_app,
allowed_methods=['GET', 'POST', 'PUT', 'PATCH', 'DELETE'], # Allowed override methods
bodyless_methods=['GET', 'HEAD', 'OPTIONS', 'DELETE'], # Methods without body
override_param='_method', # Form parameter name
header_override='X-HTTP-Method-Override' # Header name for override
)
Parameter | Type | Default | Description |
---|---|---|---|
allowed_methods |
Iterable[str] |
['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'] |
HTTP methods that can be used as overrides |
bodyless_methods |
Iterable[str] |
['GET', 'HEAD', 'OPTIONS', 'DELETE'] |
Methods that should not have a request body |
override_param |
str |
'_method' |
Form parameter name used for method override |
header_override |
str |
'X-HTTP-Method-Override' |
HTTP header name for method override |
This middleware implements several security measures:
- POST-only overrides: Method override is only allowed from POST requests
- Whitelist approach: Only explicitly allowed methods can be used as overrides
- No self-override: Cannot override a method to itself
- Body handling: Automatically removes body content for bodyless methods
- The middleware intercepts incoming WSGI requests
- Checks if the original request method is POST
- Looks for override method in:
- HTTP headers (
X-HTTP-Method-Override
by default) - checked first for performance - Form data (
_method
parameter by default) - only for POST requests
- HTTP headers (
- Validates the override method against allowed methods
- Updates the
REQUEST_METHOD
in the WSGI environ - Handles body content appropriately for bodyless methods
The middleware uses a clean, zero-dependency approach:
- Direct WSGI environ manipulation: No external dependencies required
- Stream handling: Carefully reads and reconstructs the request body stream to avoid conflicts
- Header parsing: Efficiently extracts HTTP headers from WSGI environ variables
- Form parsing: Uses Python's built-in
urllib.parse.parse_qs
for form data processing - Error resilience: Gracefully handles malformed requests without breaking the application
- RESTful APIs: Enable full REST verb support in HTML forms
- Legacy browser support: Support for older browsers that only support GET/POST
- Form-based applications: Build rich web applications with proper HTTP semantics
- API consistency: Maintain consistent API design across different client types
# Clone the repository
git clone https://github.com/marcuxyz/method-override.git
cd method-override
# Install dependencies
poetry install
# Run tests
poetry run pytest
# Run tests with coverage
poetry run pytest --cov=src/wsgi_method_override
# Format code
poetry run black .
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- π¦ Better Package Name: Renamed from
wsgi-method-override
tomethod-override
for better discoverability
- π Zero Dependencies: Completely removed Werkzeug dependency - now uses only Python standard library
- π§Ή Code Simplification: Major refactor for better readability and maintainability
- π Bug Fix: Resolved browser hanging issue when accessing
request.form
in WSGI applications - β‘ Performance: Faster form parsing with direct stream handling
- π Better Documentation: Improved code comments and documentation in Portuguese for better understanding
- π§ Improved Error Handling: More robust error handling with graceful fallbacks
- π― Cleaner API: Simplified internal methods with clear, descriptive names
- π‘ Better Debugging: Enhanced logging for troubleshooting middleware issues
- Python Compatibility: Expanded Python version support from 3.12.4 to >=3.10.0
- Broader Compatibility: Now supports Python 3.10, 3.11, and 3.12+
- Improved Accessibility: Makes the package available to more users with different Python versions
- Initial release
- Basic method override functionality
- Support for form parameters and headers
- Comprehensive test suite
- Security measures implemented
If you encounter any issues or have questions, please:
- Check the documentation
- Search existing issues
- Create a new issue if needed
- Marcus Almeida - Initial work - marcuxyz
- Inspired by similar middleware implementations in other web frameworks
- Built with Python standard library for maximum compatibility
- Follows WSGI standards and best practices