PATCH and PUT Request Does not Working with form-data(PATCH 和 PUT 请求不适用于表单数据)
问题描述
我正在使用 Laravel 创建一个 RESTFUL 应用程序,并使用 Postman 测试该应用程序.目前,如果从 Postman 发送的数据带有表单数据,则 PATCH 或 PUT 存在问题.
I am using Laravel to create a RESTFUL application and I test the application with Postman. Currently, there is an issue for PATCH or PUT if the data sent from Postman with form-data.
// Parameter `{testimonial}` will be sent to backend.
Route::post ('testimonials/{testimonial}', 'TestimonialController@update');
// Parameter `{testimonial}` will not be sent to backend (`$request->all()` will be empty) if sent from Postman with form-data.
Route::patch ('testimonials/{testimonial}', 'TestimonialController@update');
Route::put ('testimonials/{testimonial}', 'TestimonialController@update');
- 使用表单数据,
$request->all()可以用于POST. - 使用 x-www-form-urlencoded,
$request->all()对PATCH、PUT和POST. - 但是,如果我从 Postman 发送带有表单数据的
PUT和PATCH,则$request->all()将为空(参数不会发送到后端). - Using form-data,
$request->all()will be okay forPOST. - Using x-www-form-urlencoded,
$request->all()will be okay forPATCH,PUT, andPOST. - However, if I am sending
PUTandPATCHwith form-data from Postman, the$request->all()will be empty (the parameters will not be sent to backend).
目前的解决方案是使用 POST 来更新模型.我想知道为什么 PATCH 和 PUT 在使用 Postman 的表单数据发送时不起作用.
Right now the solution is to use POST for updating a model. I want to know why PATCH and PUT is not working when sent with form-data from Postman.
推荐答案
这是一个已知问题,解决方法建议如下 Github comment 是在发送 PATCH/PUT 请求时,您应该执行以下操作:
This is a known issue and the workaround suggestion as per the following Github comment is that when sending a PATCH / PUT requests you should do the following:
您应该发送 POST 并将 _method 设置为 PUT(与发送表单相同)以使您的文件可见
You should send POST and set _method to PUT (same as sending forms) to make your files visible
所以基本上你发送一个带有参数的 POST 请求,该参数设置实际方法,Laravel 似乎明白这一点.
So essentially you send a POST request with a parameter which sets the actual method and Laravel seems to understand that.
根据文档:
由于 HTML 表单无法发出 PUT、PATCH 或 DELETE 请求,您需要添加一个隐藏的 _method 字段来欺骗这些 HTTP 动词.@method Blade 指令可以为你创建这个字段:
Since HTML forms can't make
PUT,PATCH, orDELETErequests, you will need to add a hidden_methodfield to spoof these HTTP verbs. The@methodBlade directive can create this field for you:
<form action="/foo/bar" method="POST">
@method('PUT')
...
</form>
或者,您可以使用 method_field 执行上述操作的辅助函数:
Alternatively, you can use the method_field helper function to do the above:
method_field 函数生成一个 HTML 隐藏输入字段,其中包含表单的 HTTP 动词的欺骗值.例如,使用 Blade 语法:
The method_field function generates an HTML hidden input field containing the spoofed value of the form's HTTP verb. For example, using Blade syntax:
<form method="POST">
{{ method_field('PUT') }}
</form>
这篇关于PATCH 和 PUT 请求不适用于表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PATCH 和 PUT 请求不适用于表单数据
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Laravel 仓库 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
