Response header name 'Access-Control-Allow-Origin ' contains invalid characters, aborting request

Viewed 2218

I have a laravel app (deployed on heroku) where I am receiving a 500 Internal Server error.

When I checked my logs, I get the error metheod in the title:

Response header name 'Access-Control-Allow-Origin ' contains invalid characters, aborting request

I have a cors middleware with the following code:

public function handle($request, Closure $next)
{
    header('Access-Control-Allow-Origin : *');
    header('Access-Control-Allow-Headers : Content-type, X-Auth-Token, Authorization, Origin');
    return $next($request);
}

What am I doing wrong?

1 Answers

Maybe the problem is extra space before :

But in fact you should modify your middleware to do it in "Laravel way". Instead of

header('Access-Control-Allow-Origin : *');
header('Access-Control-Allow-Headers : Content-type, X-Auth-Token, Authorization, Origin');
return $next($request);

you should use:

return $next($request)
      ->header('Access-Control-Allow-Origin', '*')
      ->header('Access-Control-Allow-Headers', 'Content-type, X-Auth-Token, Authorization, Origin');
Related