Slim Framework:不允许使用的方法不允许使用的方法.必须是以下之一:POST [英] Slim Framework: Method not allowed Method not allowed. Must be one of: POST

查看:58
本文介绍了Slim Framework:不允许使用的方法不允许使用的方法.必须是以下之一:POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用PHP设置REST服务器,并希望允许客户端使用具有不同方法(例如GET,POST,PUT,DELETE等)的端点.但是,当我尝试为POST方法添加函数时存在一个问题:如果我尝试通过Postman用POST访问它,则应用程序将运行GET函数.

I'm setting up a REST-server in PHP and want to allow the client to use an endpoint with different methods like GET, POST, PUT, DELETE, ... But there is a problem when I try adding the function for the POST method: The application runs the function for GET if I try to access it with POST via Postman.

我已经尝试注释GET函数,但是如果执行此操作,则会出现错误405.

I already tried to comment the GET function but if I do this, I get an error 405.

// Just a testing function for POST
$app->post('/users', function (Request $request, Response $response, array $args)
{
    $user = $request->getParsedBody();
    $response->getBody()->write(json_encode($user->getWrapperClass()));
    return $response->withHeader('Content-Type', 'application/json');
});

有人可以帮助我吗?

推荐答案

我发现了问题:这不是邮递员,也不是我的代码.问题是在邮递员中输入的URL:这是一个http URL,服务器具有自动重定向到https的功能.在此过程中,HTTP方法只是更改为GET而不是POST,PUT或其他任何方法...现在将URL更改为https:现在可以正常使用!

I have found the problem: It was not Postman and not my code. The problem was the URL entered in Postman: It was a http URL and the server has an automatic redirection to https. During this process the HTTP method just changed to GET instead of POST, PUT or anything else... Now changed the URL to https: It works fine now!

这篇关于Slim Framework:不允许使用的方法不允许使用的方法.必须是以下之一:POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆