简体   繁体   English

使用Content-Type进行发布:multipart / form-data导致空的请求正文

[英]POSTing with Content-Type: multipart/form-data results in empty request body

Recently I started getting validation errors when POSTing to my REST interface with Content-Type: multipart/form-data . 最近,当我使用Content-Type: multipart/form-data到REST接口时,我开始收到验证错误。 Content-Type: application/x-www-form-urlencoded is working. Content-Type: application/x-www-form-urlencoded正在运行。

Here is my testing function: 这是我的测试功能:

/**
 * @Rest\Route("/testtype")
 */
public function postTypeTestAction()
{
    $request = $this->getRequest()->request->all();
    $phpContents = file_get_contents("php://input");

    return FOSView::create()->setStatusCode(200)->setData(array('request'=>$request, 'phpinput' => $phpContents));
}

When I POST using Content-Type: application/x-www-form-urlencoded : 当我使用Content-Type: application/x-www-form-urlencoded

{
  "request":{
    "test":"message"
   },
  "phpinput":"test=message"
}

When I POST using Content-Type: multipart/form-data : 当我使用Content-Type: multipart/form-data

{
  "request":[
  ],
 "phpinput":"------WebKitFormBoundaryFyQqAxqqfuhWzHUq\r\nContent-Disposition: form-data; name=\"test\"\r\n\r\nmessage\r\n------WebKitFormBoundaryFyQqAxqqfuhWzHUq--\r\n"

} }

Since there is no request data, I'm getting This value should not be blank validation errors. 由于没有请求数据,因此我得到This value should not be blank验证错误。 This breaks my application. 这破坏了我的应用程序。 I've been staring at this for so long, I'm certain I'm missing something simple. 我已经盯着看了这么久了,可以肯定我缺少一些简单的东西。

I'm using Symfony 2.3.7 and FOSRestBundle 1.0.0. 我正在使用Symfony 2.3.7和FOSRestBundle 1.0.0。

The issue resolved itself literally overnight. 这个问题实际上在一夜之间解决了。 No server reboot, no change to code, and I'm using the same tool to test ( DHC by Restlet - a Chrome extension). 没有重新启动服务器,没有更改代码,而且我正在使用同一工具进行测试( Restlet的DHC -Chrome扩展程序)。 However, since this was not working on both dev and staging environments, and is now working on both environments, this leaves me with the only answer being the testing tool, Chrome, a local caching issue or some combination. 但是,由于这在开发环境和暂存环境中均不起作用,而现在在两种环境中均有效,因此,唯一的答案就是测试工具,Chrome,本地缓存问题或某种组合。

Lesson learned: use multiple testing tools. 获得的经验:使用多种测试工具。

Now when I POST using Content-Type: application/x-www-form-urlencoded : 现在,当我使用Content-Type: application/x-www-form-urlencoded

{
  "request":{
    "test":"message"
   },
  "phpinput":"test=message"
}

Now when I POST using Content-Type: multipart/form-data : 现在,当我使用Content-Type: multipart/form-data

{
  "request":{
    "test":"message"
   },
  "phpinput":""
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 大吃一惊,如何在多部分/表单数据中强制使用内容类型 - guzzle, how to force content-type in a multipart/form-data 将文件附加为multipart / form-data内容类型-Laravel - attach file as multipart/form-data content-type - Laravel 请求类型为 multipart/form-data 的空 $_POST 和 $_FILES - empty $_POST and $_FILES with request type multipart/form-data 如果我使用 Content-Type multipart/form-data 如何获取“php://input”数据 - How to get “php://input” data if I'm using Content-Type multipart/form-data 在PHP的curl中指定multipart / form-data的每个部分的Content-Type - Specifying the Content-Type of each part of multipart/form-data in PHP's curl Laravel $request->all() 使用 multipart/form-data 为空 - Laravel $request->all() is empty using multipart/form-data 在PUT,PATCH,DELETE ...请求中使用php读取multipart / form-data的原始请求体 - Read raw request body for multipart/form-data with php in PUT, PATCH, DELETE … request 发送发布请求的多部分/表单数据 - Sending a Post request multipart/form-data 带有 multipart/form-data 的空 $request->request 和 $request->files - Empty $request->request and $request->files with multipart/form-data 在 curl 中将原始图像数据作为多部分/表单数据发布 - Posting raw image data as multipart/form-data in curl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM