Plack::Middleware::MethodOverride - Override REST methods to Plack apps via POST
In your Plack App:
use Plack::Builder; builder { enable MethodOverride; $app; };
PUT via a query parameter in your POST forms:
<form method="POST" action="/foo?x-tunneled-method=PUT"> <!-- ... --> </form>
Or override it via the x-http-method-override header in a request:
x-http-method-override
my $req = HTTP::Request->new(POST => '/foo', [ 'x-http-method-override' => 'PUT' ]);
Writing RESTful apps is a good thing, but if you're also trying to support web browsers, you're probably going to need some hackish workarounds. This module provides one such workaround for your Plack applications.
Specifically, you can also use a header named X-HTTP-Method-Override (as used by Google for its APIs) override the POST request method. Or you can add a parameter named x-tunneled-method to your form action's query. Either way, the overriding works only via POST requests, not GET.
X-HTTP-Method-Override
x-tunneled-method
If either of these attributes are available in a POST request, the REQUEST_METHOD key of the Plack environment hash will be replaced with its value. This allows your apps to override any HTTP method over POST. If your application needs to know that such overriding has taken place, the original method is stored under the plack.original_request_method key in the Plack environment hash.
REQUEST_METHOD
plack.original_request_method
The list of methods you can specify are:
If for some reason you need to use a different query parameter or header to override methods, just configure it, like so:
enable 'MethodOverride', header => 'X-HTTP-Method', param => 'my_method';
The configuration keys are:
header
Specifies the HTTP header name to specify the overriding HTTP method. Defaults to X-HTTP-Method-Override.
param
Specifies the query parameter name to specify the overriding HTTP method. Defaults to x-tunneled-method.
This module is stored in an open GitHub repository. Feel free to fork and contribute!
Please file bug reports via GitHub Issues or by sending mail to bug-Plack-Middleware-MethodOverride@rt.cpan.org.
This module gleefully steals from Catalyst::TraitFor::Request::REST::ForBrowsers by Dave Rolsky and the original version by Tatsuhiko Miyagawa (which in turn stole from HTTP::Engine::Middleware::MethodOverride). Thanks to Aristotle Pagaltzis for the shove in this direction, to Matt S Trout for suggesting that it be implemented as middleware, and to Hans Dieter Pearcey for convincing me not to parse body parameters.
David E. Wheeler <david@kineticode.com>
Copyright (c) 2010 David E. Wheeler. Some Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Plack::Middleware::MethodOverride, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Plack::Middleware::MethodOverride
CPAN shell
perl -MCPAN -e shell install Plack::Middleware::MethodOverride
For more information on module installation, please visit the detailed CPAN module installation guide.