0

I am writing the API's in Lumen. All the GET API's are working fine in ReactJS using axios. But, POST API's are not working.I enabled the cors in Lumen by creating a middleware and use them in web.php routing file. But, still getting the same error. I can't be able to debug the problem that it's from client side or server side.

<?php
/**
* Location: /app/Http/Middleware
*/
namespace App\Http\Middleware;
use Closure;

class Cors{

    /**
    * Handle an incoming request.
    *
    * @param  \Illuminate\Http\Request  $request
    * @param  \Closure  $next
    * @return mixed
    */

    public function handle($request, Closure $next){
        $headers = [
            'Access-Control-Allow-Origin'      => '*',
            'Access-Control-Allow-Methods'     => 'POST, GET, OPTIONS, PUT, DELETE',
            'Access-Control-Allow-Credentials' => 'true',
            'Access-Control-Max-Age'           => '86400',
            'Access-Control-Allow-Headers'     => 'Content-Type, Authorization, X-Requested-With'
        ];
        if ($request->isMethod('OPTIONS'))
        {
            return response()->json('{"method":"OPTIONS"}', 200, $headers);
        }
        $response = $next($request);
        foreach($headers as $key => $value)
        {
            $response->header($key, $value);
        }
        return $response;
    }
}

Register the middleware in bootstrap/app.php

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

Finally, I put the requests in middleware

$router->group(['middleware' => ['cors']], function () use ($router) {
    # Login API's
    $router->post('/login', 'LoginController@index');
    $router->post('/register', 'UserController@register');
    $router->get('/user/{id}', ['middleware' => 'auth','uses' => 'UserController@get_user']);
});

But, the above solution is not working.

---------------- ERROR --------------------- Access to XMLHttpRequest at 'http://localhost:8000/login' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 0.chunk.js:10190 POST http://localhost:8000/login net::ERR_FAILED

vikas dhiman
  • 297
  • 3
  • 19
  • You're requesting from two different ports. You need to setup a CORS middleware to allow requests from outside ports and urls. I don't know Lumen much, but tihs might help: https://www.codementor.io/chiemelachinedum/steps-to-enable-cors-on-a-lumen-api-backend-e5a0s1ecx – Baruch Aug 20 '19 at 12:56
  • I follow the same link earlier and implemented. But, getting the same error. – vikas dhiman Aug 20 '19 at 13:17
  • Thanks Guys. Problem is solved now. I installed the laravel-cors package "composer require barryvdh/laravel-cors" this solve my problem. – vikas dhiman Aug 20 '19 at 13:50

0 Answers0