0

I am using a laravel backend with a gatsby frontend. I want to fetch an image with axios http get request from the public folder of the laravel backend.

Here is my CORS middleware

        $response = $next($request);

    $response->headers->set('Access-Control-Allow-Origin' , '*');
    $response->headers->set('Access-Control-Expose-Headers' , 'Access-Control-Allow-Origin');
    $response->headers->set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, PATCH, DELETE');
    $response->headers->set('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers,X-Access-Token,XKey,Authorization');

    return $response;

And below is the fetch request

      fetch("https://quickrent.herokuapp.com/images/posts/20190506162816.jpg", {method:'GET'})
        .then(response => response.text())
        .then(result => console.log(result))
        .catch(error => console.log('error', error));
    }

2 Answers2

0

I don't see Access-Control-Allow-Origin in the response headers set. Are you sure you made it right in Laravel?

enter image description here

vitkarpov
  • 399
  • 2
  • 8
0

Try this in your Middleware:

header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: Content-Type, Accept, Authorization, X-Requested-With, X-Auth-Token, Origin, Application');
        return $next($request);

Kernel.php

protected $routeMiddleware = [
    'cors' => \App\Http\Middleware\Cors::class,

let me know if it helps! Or You can See Here Laravel 5.1 API Enable Cors

void
  • 915
  • 8
  • 20