Cannot upload file using guzzle and send to laravel API

Posted 3 years ago by jay_gorio

I have two laravel projects, one for API and one for front end. What I want to achieve is to send the uploaded file on my front end and send it to my API using guzzlehttp 6. Here's my code.

FrontEnd Laravel:

public function store(Request $request, $module_id)
{
    $videos = $request->file('file');
    // $name = time().$videos->getClientOriginalName();
    // $path = ('videos');
    // $videoFinal = $videos->move($path, $name);

    $file_name = fopen($videos, 'r');
    $client = new Client(['base_uri' => 'http://localhost/api.kourse/public/api/v1/']);

    try{
        $response = $client->request('POST', 'modules/'.$module_id.'/videos',[
            
            'file_name' => $file_name
           
        ]);

    }catch(ClientException $e){
        return $e->getResponse();
    }
    

}

When returning the $videos im getting the path of the file.

Here's my API

public function store(VideoRequest $request, $moduleId)
{

    $module = Module::find($moduleId);

    if( !$module )
    {
        return response()->json([

            'message'   => 'Module does not exist',
            'code'      => 404
        ],404);
    }

    $file = $request->file('file_name');
    $name = time(). $file->getClientOriginalName();
    $path = '/videos';
    $file->move(public_path().$path, $name);
    Video::create([
        
        'file_name' => $name,
        'file_path' => $path.$name,
        'module_id' => $module->id
    ]);

    return response()->json([

        'message'   => 'Successfully created and assgin video to this module'
    ],200);
}

What Iam getting is that :

{message: {file_name: ["The file name field is required."]}, code: 422}
code
:
422

as defined in my VideoRequest.

Can anyone help me on this.

Please sign in or create an account to participate in this conversation.