即使允许方法,流明也会抛出405错误

时间:2017-11-08 08:16:45

标签: php lumen http-status-code-405

我正在使用Lumen框架实现API,并且很难用一条麻烦的路线。我的所有其他路由都可以正常工作,但即使允许使用已使用的方法,这一条路径仍会抛出405错误。最糟糕的是,它并不总是抛出这个错误,但它确实有90%的请求。

简化的路线定义(它实际上嵌套在几个组中,您可以在下面看到):

$router->post('v1/monitoring/executions/{execution_id}/calls/{call_id}/events', 'EventController@createEvent');

然而,每当我发布某些内容时,我都会遇到405 Method not Allowed异常。检查Apache日志确实显示已收到POST请求:

  

[08 / Nov / 2017:08:05:32 +0000]" POST / v1 / monitoring / executions / 1 / calls / 1 / events HTTP / 1.1" 405 49" - " " PostmanRuntime / 6.4.1"

路由文件中此路由的完整路径为:

$router->group(['prefix' => 'v1'], function () use ($router) {

    $router->group(['prefix' => 'monitoring/executions', 'namespace' => 'Monitoring', 'middleware' => 'integration_token'], function () use ($router) {
        // POST and PUT requests for executions

        $router->group(['prefix' => '{execution_id}/calls'], function () use ($router) {
            // POST and PUT routes for calls

            $router->group(['prefix' => '{call_id}/events'], function () use ($router) {
                $router->post('/', 'EventController@createEvent');
                $router->put('{event_id}', 'Eventcontroller@updateEvent');
            });
        });
    });
});

integration_token中间件也不会返回任何405错误:

public function handle($request, Closure $next)
    {
        $headers = $request->headers->all();

        if (!isset($headers['x-integration-token'])) {
            abort(400, "X-Integration-Token header is required");
        }

        $token = $headers['x-integration-token'][0];

        $pattern = '/^[A-Za-z0-9]{32}$/';
        $pattern_matches = preg_match($pattern, $token);

        if (!$pattern_matches) {
            abort(400, "Malformed X-Integration-Token header");
        }

        try {
            $integration = Integration::where(['access_token' => $token])->firstOrFail();
            $integration_id = $integration->id;
            $request->attributes->set('integration_id', $integration_id);
        } catch (\Exception $e) {
            abort(403, "Incorrect access token: $token");
        }


        return $next($request);
    }

控制器也没有:

命名空间App \ Http \ Controllers \ Monitoring;

use App\Call;
use App\Event;
use App\Execution;
use App\Http\Controllers\Controller;
use App\Libraries\Helpers;
use Illuminate\Http\Request;

class EventController extends Controller
{
    public function createEvent(Request $request, $execution_id, $call_id) {
        $input = $request->all();
        $input['execution_id'] = $execution_id;
        $input['call_id'] = $call_id;
        $input['time_string'] = Helpers::getTimestampTz();
        $input['time'] = Helpers::getMicroTime();

        Helpers::validateRequiredFields(Event::class, $input);

        // Verify execution and call exist
        Execution::findOrFail($execution_id);
        Call::findOrFail($call_id);

        $event = Event::create($input);

        return response()->json($event)->setStatusCode(201);
    }

    // Other methods
}

老实说,我不知道可能导致这个问题的原因,但它让我发疯了!任何帮助解决这个问题都将非常感激。

0 个答案:

没有答案