简体   繁体   中英

Override HTTP method with Play framework

Some web frameworks offer the possibility to "override" the HTTP method with a hidden form field:

<form method="POST">
  <input type="hidden" type="_method" value="PUT">
  ...
</form>

Is it possible to override the HTTP method with the Play framework?

NOTE: This is for Play 1.x only.

Looking at the source code for the Router.route() method, it uses the x-http-method-override parameter in the query string. According to this bug report you should also be able to achieve this using HTTP Headers, but I couldn't see this in the source code.

To get it to work, you need to add the override to the request string. I ran the following example to get it to work.

<form action="@{Application.form}?x-http-method-override=PUT" method="POST">
  <input type="submit" type="go" value="go">
</form>

If you set the logging level to TRACE, you will see the following output, showing the method has changed. You should see an output like --

08:20:34,855 TRACE ~ init: begin
08:20:34,858 TRACE ~ Route: /application/form - x-http-method-override=PUT
08:20:34,859 TRACE ~ request method POST overriden to PUT
08:20:34,860 TRACE ~ ------- public static void controllers.Application.form()
08:20:34,861 TRACE ~ init: end true

You can also check the request.method in your controller to confirm.

In Play2, this is not possible and the creators have indicated that there is no plan to support it. You'll have to manually intercept the request and change the HTTP method by overriding Global.onRouteRequest .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM