简体   繁体   English

使用Play框架覆盖HTTP方法

[英]Override HTTP method with Play framework

Some web frameworks offer the possibility to "override" the HTTP method with a hidden form field: 一些Web框架提供了使用隐藏表单字段“覆盖”HTTP方法的可能性:

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

Is it possible to override the HTTP method with the Play framework? 是否可以使用Play框架覆盖HTTP方法?

NOTE: This is for Play 1.x only. 注意:这仅适用于Play 1.x.

Looking at the source code for the Router.route() method, it uses the x-http-method-override parameter in the query string. 查看Router.route()方法的源代码 ,它在查询字符串中使用x-http-method-override参数。 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. 根据此错误报告,您还应该能够使用HTTP标头实现此目的,但我在源代码中看不到这一点。

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. 如果将日志记录级别设置为TRACE,您将看到以下输出,显示方法已更改。 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. 您还可以检查控制器中的request.method进行确认。

In Play2, this is not possible and the creators have indicated that there is no plan to support it. 在Play2中,这是不可能的,创作者已表示没有计划支持它。 You'll have to manually intercept the request and change the HTTP method by overriding Global.onRouteRequest . 您必须手动拦截请求并通过覆盖Global.onRouteRequest更改HTTP方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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