fruitcake / laravel-cors

Adds CORS (Cross-Origin Resource Sharing) headers support in your Laravel application
MIT License
6.27k stars 614 forks source link

405 Error on Angular4 call #264

Closed pbvarsok closed 6 years ago

pbvarsok commented 6 years ago

*Lumen (5.5.2) (Laravel Components 5.5.)**

"php": ">=5.6.4",
"laravel/lumen-framework": "5.5.*",
"vlucas/phpdotenv": "~2.2",
"tymon/jwt-auth": "1.0.0-rc.1",
"barryvdh/laravel-cors": "^0.10.0"

bootstrap/app.php

<?php

require_once __DIR__.'/../vendor/autoload.php';

try {
    (new Dotenv\Dotenv(__DIR__.'/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
    //
}

$app = new Laravel\Lumen\Application(
    realpath(__DIR__.'/../')
);

$app->withFacades();
$app->withEloquent();

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

$app->middleware([
   \Barryvdh\Cors\HandleCors::class,
]);

$app->routeMiddleware([
    'auth' => App\Http\Middleware\Authenticate::class,
]);

$app->register(App\Providers\AuthServiceProvider::class);
$app->register(Tymon\JWTAuth\Providers\LumenServiceProvider::class);
$app->register(Barryvdh\Cors\LumenServiceProvider::class);

$app->configure('cors');

$app->router->group([
    'namespace' => 'App\Http\Controllers',
], function ($router) {
    require __DIR__.'/../routes/web.php';
});

return $app;

routes/web.php

<?php

Route::group(['prefix' => 'test'], function ($router) {

    Route::post('user', function(){
        return ['lumen' => 'yes'];
    });

});

config/cors.php

<?php

return [

    'supportsCredentials' => false,
    'allowedOrigins' => ['http://localhost:4200'],
    'allowedHeaders' => ['Content-Type', 'Accept', 'Authorization', 'X-Requested-With', 'Access-Control-Allow-Credentials'],
    'allowedMethods' => ['GET', 'POST', 'OPTIONS', 'DELETE'],
    'exposedHeaders' => ['Authorization'],
    'maxAge' => 0,

];

From console: curl -X POST 'http://api.plateabar.lan/test/user?e=3&a=1' I get the response OK.

From Postman, I get the response OK.

But when I make it from Angular4 I get: OPTIONS http://api.local.lan/test/user 405 (Method Not Allowed) tests:1 Failed to load http://api.local.lan/test/user: Response for preflight has invalid HTTP status code 405

captura de pantalla 2017-12-04 a la s 11 21 22

rafadpedrosa commented 6 years ago

@pbvarsok i'm facing the same issue.

Please, if you fix it respond here!

I'll do the same :)

pbvarsok commented 6 years ago

Sorry @rafadpedrosa, I've removed barryvdh/laravel-cors and installed "nordsoftware/lumen-cors": "^2.2" All works fine with that package.

barryvdh commented 6 years ago

Can you try with 0.10.1?

jnovermars commented 6 years ago

@barryvdh for me 0.10.1 is the fix in Lumen 5.4

barryvdh commented 6 years ago

Nice :)