如何在具有文件上传功能的Laravel API中使用PUT方法 [英] How to use PUT method in Laravel API with File Upload
问题描述
已阅读链接,
PUT
最常用于更新功能,将请求体包含到最新的原始资源表示中,将其PUT到已知资源URI.
PUT
is most-often utilized for update capabilities, PUT-ing to a known resource URI with the request body containing the newly-updated representation of the original resource.
...我们需要再次发送 所有 数据参数.
... we need to send all the parameters of the data again.
在我的控制器中,我有:
In my controller, I have:
$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
,但这不允许文件上传.
I have once used this same code for POST
method and it worked, but while using it for APIs, I used POSTMAN form-data
and got $request->all()
to be null
. Some said I should use x-www-form-urlencoded
but this does not allow files upload.
推荐答案
这实际上是 PHP本身没有能力.只是带有 multipart/form-data
的PUT/PATCH请求不会填充 $ _ FILES
,因此Laravel无法使用.
This is actually an incapability of PHP itself. A PUT/PATCH request with multipart/form-data
just will not populate $_FILES
, so Laravel has nothing to work with.
文件最好以 multipart/form-data
发送,并且如果是POST,则只会填充 $ _ FILES
.
Files are best sent as multipart/form-data
and that will only populate $_FILES
if it's a POST.
显然, $ request-> all()
返回null的事实是
Apparently the fact that $request->all()
returns null is a known issue in Laravel.
如果在Laravel中解决此问题,则可以使用POST.
In lieu of fixing this in Laravel, if it works using a POST, just use a POST.
这篇关于如何在具有文件上传功能的Laravel API中使用PUT方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!