Closed AbdullahGhanem closed 4 years ago
Are you also adding headers with php or nginx directly?
no, I just use laravel valet. and laravel 7.
@barryvdh plz can help me?
Hi @barryvdh I have the same issue
Hi @barryvdh @AbdullahGhanem, my issue was with the paths array.
I setted the full url instead of set just the paths
Hi @barryvdh @AbdullahGhanem, my issue was with the paths array.
I setted the full url instead of set just the paths
Could you provide an example for this?
Hi, a have passed to the same problems when update my laravel to 5.8 to 6.*, using v1.0.5 from laravel-cors. add to config/app.php providers => [ ... \Fruitcake\Cors\CorsServiceProvider::class ]
Hi @CesarGomezTissini I was not using the api/
prefix for the api but I was using the full domain api.site.ext/
.
The paths
array was setted with api.site.local/
and this is wrong, I updated with'*'
it's the path with out the domain and now works fine.
The paths should contains the prefix of your api
route group.
The default api route group prefix is api
, so the default path
config contains only api/*
.
This is my cors.php file:
<?php
return [
/*
|--------------------------------------------------------------------------
| Cross-Origin Resource Sharing (CORS) Configuration
|--------------------------------------------------------------------------
|
| Here you may configure your settings for cross-origin resource sharing
| or "CORS". This determines what cross-origin operations may execute
| in web browsers. You are free to adjust these settings as needed.
|
| To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
|
*/
'paths' => ['*'],
'allowed_methods' => ['*'],
'allowed_origins' => [
'https://site.local',
],
'allowed_origins_patterns' => [],
'allowed_headers' => [
'*',
],
'exposed_headers' => [
'*',
],
'max_age' => 0,
'supports_credentials' => true,
];
Hi,
same problem. Ok if I put my url it works fine, but the of "*" is that works for all domain. Is it a bug from last Laravel version ?
What do you mean? ['*']
doesn't work? Do you need the credentials? Do you get any headers back?
If so, please update to v2 and create a new issue with all details.
The suggestion of @dhavaldav fixed this issue for me. This is not a bug of the laravel-cors package, but more of a misunderstanding of how to configure it properly. Looks like some (or all?) requests to the laravel backend use multiple headers which are not allowed by the default laravel-cors configuration 'allowed_origins' => ['*']
, so you have to update that to allowed_origins => ['*,*']
to allow that.
it is work for me, with multiple
*,*
on allowed_origins At config/cors.php'allowed_origins' => ['*,*'],
This solution work for me, thanks.
Hi guys,
TL;DR; Check your .htaccess, you might already have added the CORS headers in it .
@dhavaldav solutions works; but not for the reasons mentioned here (I think) ; let me explain,
I was facing the same issue on my app hosted on Heroku, When I was curling my app on Heroku ; I saw 2 headers (curl -vI https://)
HTTP/1.1 200 OK
Connection: keep-alive
Date: Tue, 01 Dec 2020 15:11:44 GMT
Server: Apache
Cache-Control: no-cache, private
X-Ratelimit-Limit: 60
X-Ratelimit-Remaining: 59
Access-Control-Allow-Origin: * # here
Access-Control-Allow-Origin: * # and here
Access-Control-Allow-Methods: *
Access-Control-Allow-Headers: *
Content-Type: application/json
Via: 1.1 vegur
That was causing the "header contains multiple values" error message.
But on local, I only had one header
curl -I http://localhost:8000/api/finders/5f64b539e623250dc5473762
HTTP/1.1 200 OK
Host: localhost:8000
Date: Tue, 01 Dec 2020 15:12:27 GMT
Connection: close
X-Powered-By: PHP/7.4.11
Cache-Control: no-cache, private
Date: Tue, 01 Dec 2020 15:12:27 GMT
Content-Type: application/json
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 58
Access-Control-Allow-Origin: *
That's because Apache 2 is the server running on Heroku ; but on local it's only the php artisan server
The issue is : I had this in my .htaccess (.htaccess is a configuration file used by Apache)
Header add Access-Control-Allow-Origin: "*"
Header add Access-Control-Allow-Methods: "*"
Header add Access-Control-Allow-Headers: "*"
So basically, the issue is that the header Access-Control-Allow-Origin is added twice . First by the .htaccess (parsed by Apache) Then Again by the Laravel/cors something feature.
Adding 'allowed_origins' => ['*,*'],
is actually an error of syntax, which causes the Laravel package to skip that configuration line, and ends up not adding the header to the request.
Want to test ? Try another incorrect value, like '*,
' or '*,*,*'
. The result will (should) be the same .
So basically, what we're doing, is making the Laravel package skips the allowed_origins configuration ; so .htaccess is the only one who adds the Header . Result : we only have 1 header, et voilà !
Now, a nicer way to achieve the same result, is to leave the array empty 'allowed_origins' => [],
(Careful ; I tried with false as a value ; but some other headers disappears as well ; so I wouldn't recommend it)
That's what solved the issue for me ! Let me if that works for you
Versions laravel/framework : v6.18.38 barryvdh/laravel-cors: v2.0.1
Yep, just lost ~20 minutes myself. A previous developer added the following to the API htaccess file:
// try to sort cors issues
Header set Access-Control-Allow-Origin "*"
# Header set Access-Control-Allow-Origin "all"
# Header set Access-Control-Allow-Origin ""
Removing tall that of course solved the issue :+1:
first I did all the instructions that I set before I created this issue in nuxt project, I get this error.
this OPTIONS method request:
my Kernel.php
my config/cors.php
this error when use all methods get, post, patch ..