3

I want to update/upload an image using http client laravel. I did update the data but the image are not updated. the API are blob type. I try to use form-data file in postman and it did work. but I don't know why it's not working in my laravel app.

this is my code

$image = $request->file('profile_image');
        if ($image != null) {
            $contents = $image->openFile()->fread($image->getSize());
        }
        $response = Http::withToken($request->session()->get('user'))->acceptJson()->attach('image', file_get_contents($image))->post("https://voice.infidea.id/api/gateway/update-user", [
            'id' => $user['id'],
            'name' => $request->name,
            'email' => $request->email,
            'contact' => $request->contact
        ]);

I try to use the $content also but still did not work. only the string data that updated.

1
  • in my postman it's ok no problem. mine in in the code when to post the API from code Commented Aug 28, 2021 at 9:46

2 Answers 2

2

You need to specify complete file path for file_get_contents(), change to this

file_get_contents($request->file('profile_image')->getRealPath()));
// or
file_get_contents($request->profile_image->path());

You just place the temp path or complete path of image

$contents = file_get_contents($request->file('profile_image')->getRealPath());

$response = Http::withToken($request->session()->get('user'))
    ->acceptJson()
    ->attach('image', $contents)
    ->post("https://voice.infidea.id/api/gateway/update-user", [
            'id' => $user['id'],
            'name' => $request->name,
            'email' => $request->email,
            'contact' => $request->contact
    ]);
Sign up to request clarification or add additional context in comments.

Comments

1

I think value of $image = $request->file('profile_image'); is not set bcoz in postman you use image.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.