【问题标题】:Why does my PUT HTTP request get converted to a GET为什么我的 PUT HTTP 请求被转换为 GET
【发布时间】:2016-05-14 09:21:05
【问题描述】:

我们创建了基于 PHP 的插件,该插件提供了一个 RESTful 界面,供我们正在开发的移动应用程序使用。该插件是自托管的,这意味着任何想要使用移动应用程序的人都必须在他们的服务器上安装这个 PHP 插件。我们的一位用户报告说,安装插件后他们无法登录。使用 Postman 练习登录请求中涉及的 API,我想出了以下内容:

请求

PUT http://<user's website>/index.php/wurrd/clientinterface/operator/login
{"username":"apple", "password":"dove", "clientid":"AABBB-CCCCC-DEFGHIJ", "deviceuuid":"aaa-uuu-isdi"}

回复

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
    <title>501 Method Not Implemented</title>
</head>
<body>
    <h1>Method Not Implemented</h1>
    <p>GET to /chat/index.php/wurrd/clientinterface/operator/login not supported.
        <br />
    </p>
    <p>Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body>
</html>

API 不会为 GET 公开该路由,因此当尝试通过 GET 到达那里时,错误消息是合适的。但是,问题在于请求是使用 PUT 发出的。

我不知道如何调查这个问题。服务器正在运行 Apache。 .htaccess 可能有问题吗?我认为可能是 mod_rewrite 问题,但我想这应该处理 URL 而不是 HTTP 方法。

另外,路由是使用 Symfony2 路由组件完成的。

任何帮助将不胜感激。

谢谢。

更新 1:

这是 Symfony 路由的配置:

wurrd_client_operator_login:
    # The login request details are passed in as a JSON payload.
    path: /wurrd/clientinterface/operator/login
    defaults:
        _controller: Wurrd\Mibew\Plugin\ClientInterface\Controller\OperatorController::loginAction
    methods: [PUT]

【问题讨论】:

  • 检查网络服务器日志文件
  • @CharlotteDunois,我会检查我们是否可以提取一些日志文件。

标签: php apache symfony http


【解决方案1】:

如果有一个未被注意到的 303 重定向(如果您的客户端没有正确实现 HTTP,也可能是 301/302),或者如果代理/中间人正在转换请求之前,也可能会发生这种情况它到达服务器。

要使用_method,请确保没有禁用支持: http://symfony.com/doc/current/reference/configuration/framework.html#http-method-override

【讨论】:

    【解决方案2】:

    尝试在 URL 中添加 _method 参数以强制使用该方法。

    更多信息在这里: http://symfony.com/doc/current/cookbook/routing/method_parameters.html

    【讨论】:

    • +1 但我想说,在defaults 路由配置中添加_method 参数,而不是使用methods
    • @Mauro,感谢您的回复。我将与用户进行测试并提供反馈。
    猜你喜欢
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 2019-12-20
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    相关资源
    最近更新 更多