David E. Wheeler

Name

Plack::Middleware::MethodOverride - Override REST methods to Plack apps via POST

Synopsis

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:

  my $req = HTTP::Request->new(POST => '/foo', [
      'x-http-method-override' => 'PUT'
  ]);

Description

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.

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.

The list of methods you can specify are:

GET
POST
PUT
DELETE
OPTIONS
TRACE
CONNECT

Configuration

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:

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.

Support

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.

Acknowledgements

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.

Author

David E. Wheeler <david@kineticode.com>

Copyright and License

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.




Hosting generously
sponsored by Bytemark