1

最近,我在使用Content-Type: multipart/form-data. Content-Type: application/x-www-form-urlencoded正在工作中。

这是我的测试功能:

/**
 * @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));
}

当我使用 POST 时Content-Type: application/x-www-form-urlencoded

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

当我使用 POST 时Content-Type: multipart/form-data

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

}

由于没有请求数据,我收到This value should not be blank验证错误。这打破了我的申请。我已经盯着这个很久了,我确定我错过了一些简单的东西。

我正在使用 Symfony 2.3.7 和 FOSRestBundle 1.0.0。

4

1 回答 1

1

这个问题一夜之间就解决了。无需重新启动服务器,无需更改代码,并且我使用相同的工具进行测试(Restlet的 DHC - Chrome 扩展程序)。但是,由于这不适用于开发环境和暂存环境,并且现在在两种环境中都可以使用,这让我唯一的答案是测试工具、Chrome、本地缓存问题或某种组合。

经验教训:使用多种测试工具。

现在当我使用 POST 时Content-Type: application/x-www-form-urlencoded

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

现在当我使用 POST 时Content-Type: multipart/form-data

{
  "request":{
    "test":"message"
   },
  "phpinput":""
}
于 2013-11-27T17:03:49.240 回答