Expand description

An Actix Web middleware that allows you to reroute POST requests to other methods like PUT or DELETE using a query parameter.

This is useful in HTML forms where you can’t use methods other than GET or POST. By adding this middleware to your server, you can submit the form to endpoints with methods other than POST by adding a query parameter like /your/url?_method=PUT.

For example, in the HTML:

<form method="post" action="/path/to/endpoint?_method=DELETE">
  <input type="submit" value="Delete this item" />
</form>

Then in your rust code:

App::new()
     .wrap(QueryMethod::default())
     // ...

The middleware will strip off the _method query parameter when rerouting your request, so the rerouting is transparent to your server code.

Note that this middleware only applies to POST requests. Any other request like GET or HEAD will not be changed, because it would risk opening the server up to XSRF attacks. Requests like PUT and DELETE are also not changed because the parameter was likely included accidentally. By default the middleware will allow these requests to continue to your server unchanged, but you can enable the strict_mode parameter to reject such requests.

The middleware will also reject any request where the method parameter specifies an invalid method that Actix Web doesn’t accept. You can use custom HTTP methods like LIST, but not LIST:ITEMS. See the HTTP spec for details.

This middleware uses tracing for logging. It will log warning events for bad requests (for example, GET request with method parameter), and will log debug events for good requests that have been modified by the middleware. If you prefer the log crate for your logging, you can enable it with the logging_log feature. You can also disable logging entirely.

# To use `log` for logging
actix-web-query-method-middleware = { version = "1.0", default-features = false, features = ["logging_log"] }
# To disable logging entirely
actix-web-query-method-middleware = { version = "1.0", default-features = false }

Structs