I am facing a problem with "A ‘contents’ key is required". I have been searching through but does not get the right answers. Here is my code
$response = Http::withHeaders([
'Accept'=>'application/json',
]);
foreach($vault->ad_images as $image){
if(Storage::exists($image)){
$path = Storage::path($image);
$file = fopen($path,'r');
$response = $response->attach('images[]',$file);
}
}
$response = $response->post($apiUrl,[$data]);
$response = json_decode($response->body(),true);
I have searching through about the problem, it said the the contents is required where the files is actually exists in the storage. I even trace the fopen and get this result
stream resource @25 ▼ // app/Http/Controllers/Agc/PropertyVaultController.php:564
timed_out: false
blocked: true
eof: false
wrapper_type: "plainfile"
stream_type: "STDIO"
mode: "r"
unread_bytes: 0
seekable: true
uri: "/home/ita/public_html/v2/storage/app/public/uploads/images/2023/11/291/395048-1700795192.jpg"
options: []
}
This is really confusing since its just actually happen. Last time it works without any problem with this code.
2
Answers
attach
method acceptsname
andcontents
(string value), you passresourse
.From laravel docs and from api doc, this method you have 4 parameters. They are these:
1º
string|array
$name
-> name of the file2º
string|resource
$contents
-> its contents. //ex:file_get_contents('photo.jpg')
orfopen('photo.jpg', 'r');
3º
string|null
$filename
-> file’s filename4º
array
$headers
-> // ex:['Accept'=>'application/json']