Slim Framework: Method not allowed Method not allowed. Must be one of: POST

元气小坏坏 提交于 2021-01-20 07:11:14

问题


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.

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');
});

Anyone can help me?


回答1:


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!



来源:https://stackoverflow.com/questions/55528956/slim-framework-method-not-allowed-method-not-allowed-must-be-one-of-post

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!