【问题标题】:Could not submit form-data through postman put request无法通过邮递员 put 请求提交表单数据
【发布时间】:2018-12-20 11:20:58
【问题描述】:

我正在使用 lumen api 5.6。 这是我的路线 $router->PUT('collections/{collectionId}' 在邮递员中,我像这样调用这个 api。

这是该请求的正文

请看这里,我在这里有 CollectioPoints,但验证失败。 我通过 formdata 发送的数据在验证中无法识别。

但是,如果我将路由从 put 发送到 post [$router->POST('collections/{collectionId}'],我通过表单数据发送的所有数据都会被识别,并且我会得到正确的响应,如下所示

为什么会这样。我不能通过 put 请求发送表单数据吗? 谢谢。

【问题讨论】:

  • 尝试添加到标题 Content-Type:multipart/form-data
  • 我试过@J.Doe 它也不起作用

标签: php laravel lumen


【解决方案1】:

Laravel(Lumen) 作弊,因为 html 表单只支持 GET 和 POST,但它确实理解真正的 PUT/PATCH 请求。

在 Postman 中您应该发送 POST 并将 _method 设置为 PUT(与发送表单相同)以使您的数据和文件可见

 _method = "PUT"

或者

如果您没有任何文件只有数据,那么您可以在 Postman 中选择以下选项中写入参数

 x-www-form-urlencode 

或者

您想使用 PUT 方法通过 form-Data 选项发送数据,您应该使用类似 https://gist.github.com/devmycloud/df28012101fbc55d8de1737762b70348

的东西

【讨论】:

    【解决方案2】:

    例子

    标头:Content-Type application/x-www-form-urlencoded

    _method = "PUT"

    你的控制器

    return $request->file('avatar');
    

    图片示例

    【讨论】:

    • 我必须把 (_method = "PUT") 放在哪里??
    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    相关资源
    最近更新 更多