skip to Main Content

Im trying to make a call on an endpoint where the user uploads an image. I would like to return an image from the call, not a json. This is a Post request and Im using the Laravel HTTP Client methods.

Here’s my code :

class PostImageController extends Controller
{
    /**
     * @param Request $request
     */
    public function scan(Request $request)
    {
        $url = env('AZURE_FUNCTION_PYTHON_DOCUMENT_SCANNER_URL');
        $file = $request->file('document')->get();

        $response = Http::withHeaders([
            'Content-Type' => 'multipart/form-data',
        ])->attach(
            'document',
            $file,
            'image.jpeg',
        )->post($url, $request->all());

        // Change this line to return an image
        return $response->json();
    }
}

Any idea is welcome.

2

Answers


  1. You need to stream the file given from returned URL from response (if it’s there)

    return response()->file($fileURLGivenFromHttpResponse);
    
    Login or Signup to reply.
  2. In order to return an image, you will need something like this

    return Response::make($image, 200)->header('Content-Type', $type);
    

    where $image holds the image that you want to upload and $type holds image type (for example image/png)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search