I'm getting my get requests blocked by CORS policy when downloading an image from a url with the code:
await get(product.imageUrl).then((img) async {
print('image downloaded');
var dwnldProd = product;
dwnldProd.productImage = img.bodyBytes;
await _productRepository.saveProduct(dwnldProd);
}).catchError((e) {
print('downloading product image error: $e');
});
As the No 'Access-Control-Allow-Origin' header is present on the requested resource.
says my request was missing the headers so I added them as :
await get(product.imageUrl, headers: <String,String>{
'Access-Control-Allow-Origin': '*', // Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.
// 'Access-Control-Allow-Origin':'http://localhost:5000/', // Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.
"Access-Control-Allow-Methods": "GET, POST, OPTIONS, PUT, PATCH, DELETE",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept"
}).then((img) async {
print('image downloaded');
var dwnldProd = product;
dwnldProd.productImage = img.bodyBytes;
await _productRepository.saveProduct(dwnldProd);
}).catchError((e) {
print('downloading product image error: $e');
});
but now the error is access-control-allow-origin is not allowed.
I tried so many parameters combinations from various accepted answers here on SO but none is working in my case.
Can you spot what's wrong in my headers?
Worth to mention I'm running the web app on Chrome in release mode
with the command flutter run -d chrome --release --web-hostname localhost --web-port 5000
.
Thank you very much for the help.
UPDATE
I've notice this and I don't understand it:
If I supply access-control-allow-origin
in the request header I get the error field access-control-allow-origin is not allowed
:
but if I comment it out then the error is No 'access-control-allow-origin' header is present on the requested resource
:
As the resource is a file on firebase storage, am I supposed to create this not found header on the resource when I upload the image? If so how to? In the metadata perhaps?