Home > Mobile >  cors middleware and get error on post api
cors middleware and get error on post api

Time:12-15

i made a cors middleware and set it for all apis for preventing cors error and this is my middleware :

$response = $next($request);
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, DELETE, PATCH, PUT');
$response->headers->set('Access-Control-Allow-Headers', 'Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type');
$response->headers->set('Access-Control-Allow-Credentials', 'true');
return $response;

my problem is the headers seems to work just for GET api and and on post api i m getting cors error:

it there any config on nginx side or even vue js api call for fix this problem?

CodePudding user response:

If you want to handle cors in a right way, It's better to install fruitcake/laravel-cors package, and then put this:

\Fruitcake\Cors\HandleCors::class,

in App\Http\Kernel.php like this:

protected $middleware = [
        \Fruitcake\Cors\HandleCors::class,
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];


  • Related