Encountering 403 Forbidden Error When Fetching Image in API via URL in Laravel App - How to Resolve?

50 views Asked by At

I'm facing a challenge while attempting to fetch an image via URL from my Laravel application. The specific error I'm encountering is a 403 Forbidden status code. Here are the details of the request:

Request URL: https://bpds.thedatech.com/public/upload/all/banner/202403030945WhatsApp-Image-2023-07-07-at-12.39.03-AM.jpeg
Request Method: GET
Status Code: 403 Forbidden
Referrer Policy: strict-origin-when-cross-origin**

I've verified that the URL is correct and that the image exists at the specified location. Despite this, I'm still encountering the 403 Forbidden error. Can anyone provide insights into why this error might be occurring and suggest potential solutions to resolve it? Any assistance would be greatly appreciated. I am trying this code in my CORS-Middleware:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class CorsMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
         $response = $next($request);

        // Add CORS headers
        $response->header('Access-Control-Allow-Origin', 'https://datech2bpds.netlify.app');
        $response->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
        $response->header('Access-Control-Allow-Headers', 'Content-Type, Authorization');

        return $response;
    }
}

This is my Controller CODE:

public function blogs_all_api()
{
    $blogs = Blog::join('blog_categories','blog_categories.id','blogs.category_id')
        ->select('blog_categories.name','blogs.*')
        ->get();

    foreach($blogs as $blog) {
        $blog->banner = asset('/public/upload/all/banner/' . $blog->banner);
        // $blog->date = formatDate($blog->created_at);
        // Assuming $blog->created_at is a date string
        $dateString = $blog->created_at;

        // Convert to Carbon instance
        $carbonDate = \Carbon\Carbon::parse($dateString);
        $blog->date=$carbonDate;
    }

    $response =  response()->json([
        'code'=>'success',
        'blogs'=>$blogs
    ]);

    // Set CORS headers to allow requests from 'https://digitize4life.com'
    $response->header('Access-Control-Allow-Origin', 'https://datech2bpds.netlify.app')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
        ->header('Access-Control-Allow-Headers', 'Content-Type, Authorization');

    return $response;
}
0

There are 0 answers