PATCH 和 PUT 请求不适用于表单数据 [英] PATCH and PUT Request Does not Working with form-data

查看:19
本文介绍了PATCH 和 PUT 请求不适用于表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Laravel 创建一个 RESTFUL 应用程序,并使用 Postman 测试该应用程序.目前,如果从 Postman 发送的数据带有表单数据,则 PATCHPUT 存在问题.

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()PATCHPUTPOST.
  • 但是,如果我从 Postman 发送带有表单数据的 PUTPATCH,则 $request->all() 将为空(参数不会发送到后端).
    • Using form-data, $request->all() will be okay for POST.
    • Using x-www-form-urlencoded, $request->all() will be okay for PATCH, PUT, and POST.
    • However, if I am sending PUT and PATCH with form-data from Postman, the $request->all() will be empty (the parameters will not be sent to backend).
    • 目前的解决方案是使用 POST 来更新模型.我想知道为什么 PATCHPUT 在使用 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 表单无法发出 PUTPATCHDELETE 请求,您需要添加一个隐藏的 _method 字段来欺骗这些 HTTP 动词.@method Blade 指令可以为你创建这个字段:

      Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need to add a hidden _method field to spoof these HTTP verbs. The @method Blade 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 请求不适用于表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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