Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

amitsolanki24_'s avatar

Upload files using Http client laravel

How can i upload images using http client

Here is my code

       Http::attach('attachment', file_get_contents($image), 'image.jpg')
        ->post(url('api/upload'));

Or

        Http::attach(
                 'attachment', 
                 file_get_contents($image),
                 'image.jpg', 
                ['Content-type' =>'image/jpg']
       ) ->post(url('api/upload'));

none of these are working

I have also tried with curl but not working.

0 likes
12 replies
gych's avatar

Why are you calling your api upload route from the same back end? Can't you just process the upload by calling function to upload the file?

2 likes
martinbean's avatar

@amitsolanki24_ No one can help with “not working”. Please tell us what actually happens.

Also, as @gych says, it’s completely redundant to call your own API from a server-side route. Why are you doing this, instead of just uploading the file using $request->file('attachment')->store() or whatever?

1 like
gych's avatar

@amitsolanki24_ Yes but not everything the client wants is the best solution. As a developer you should advise your client on what the best practice is.

And its also not needed to use url() helper when you're calling the route from the same server you can just use the url path.

->post('/api/upload');
1 like
martinbean's avatar

@amitsolanki24_ Well tell your client it’s completely redundant to make a HTTP request to their application, just for their application to then start another HTTP request… to itself 🤦‍♂️

1 like
amitsolanki24_'s avatar

@gych okay thanks for your suggestion 👍 now I will not use url() function again like above for the same server.

gych's avatar

@amitsolanki24_ No problem but how did you solve your issue? Did you directly call the function instead of using the Http post ?

1 like
amitsolanki24_'s avatar

@gych when I use Http::get('api/tests') this giving no url dound something

But when I use Http::get(url('api/tests')) Its working correctly

gych's avatar

@amitsolanki24_ But didn't you call the function directly instead of using the http request?

From where and why are you executing this http request?

For the URL not working, try to add / at the start, like this:

Http::get('/api/tests'))

But again you should just call the function directly instead of using this Http request on the same server.

Please or to participate in this conversation.