gpt4 book ai didi

laravel - 如何通过文件上传在 Laravel API 中使用 PUT 方法

转载 作者:行者123 更新时间:2023-12-04 11:51:46 25 4
gpt4 key购买 nike

已阅读此 SO link ,PUT最常用于更新功能,PUT 到已知资源 URI,请求正文包含原始资源的最新更新表示。
From this answer ,

... we need to send all the parameters of the data again.


在我的 Controller 中,我有:
$student = Student::find($student_id);
$student->username = $request->username;
$student->email = $request->email;
$student->password = $request->password;
$path = $request->file('passport')->store('upload');
$student->passport = $path;
我曾经为 POST 使用过相同的代码方法并且它有效,但是在将它用于 API 时,我使用了 POSTMAN form-data并得到 $request->all()成为 null .有人说我应该用 x-www-form-urlencoded但这不允许文件上传。

最佳答案

这实际上是一个 incapability of PHP itself .带有 multipart/form-data 的 PUT/PATCH 请求只是不会填充 $_FILES ,所以 Laravel 没有什么可使用的。
每隔一段时间,人们就会报告错误 like this当他们找到 $request->all()返回null,以为是Laravel 的错,Laravel 也无可奈何。
文件最好发送为 multipart/form-data那种请求将 只有填充 $_FILES如果是 发布 .否 $_FILES , 否 $request->file() .
代替在 PHP 中按预期进行这项工作,如果它使用 POST 工作,只需使用 POST。

关于laravel - 如何通过文件上传在 Laravel API 中使用 PUT 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65008650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com