Reputation: 343
I'm using Laravel 5.4 and Guzzle to upload files and images to another server.
I am using the following method on the client/server side
public function store (Request $request)
{
$dataForm = $request->all();
$Client = new Client();
$response = $Client->post('http://localhost/WebService/public/api/client',
[
'multipart' =>
[
[
'name' => 'UploadIMG',
'filename' => 'image_org',
'Mime-Type' => 'image_mime',
'contents' => $ request->file('UploadIMG'),
],
]
]);
$ response = $ response-> getBody () -> getContents ();
return response () -> json (['error' => $ response]);
}
In the documentation part of the contents is 'contents' => fopen ('/ path / to / file', 'r') but I do not want to save the image but already get the request to be able to send, how is this possible? the way I'm using comes a corrupted file in the web service. I ran some tests with postman, so the problem is in the guzzle
Upvotes: 0
Views: 1167
Reputation: 1167
With your current code for Guzzle, you're passing the $request->file('UploadIMG')
which is an instance of Illuminate\Http\UploadedFile
. For the 'contents' of the HTTP request, you need to pass the actual file itself into it.
Because the UploadedFile eventually extends SplFileInfo, my guess is to use its method of $request->file('UploadIMG')->openFile()
. If you can't read it from there, then my second guess would be to save it temporarily then read that.
Upvotes: 3