如何在具有文件上传功能的Laravel API中使用PUT方法 [英] How to use PUT method in Laravel API with File Upload

查看:80
本文介绍了如何在具有文件上传功能的Laravel API中使用PUT方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已阅读链接

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆