当多方请求时,Fosrestbundle主体为空 [英] Fosrestbundle body empty when multipart request
问题描述
在下面的代码中,我希望$request->getContents()
获得HTTP
请求的正文内容.发送非多部分请求时,这可以按预期工作,尽管使用多部分请求时,$body
变量保持为空.
In the code bellow I expect the $request->getContents()
to get the body content of the HTTP
request. When sending non multipart request this works as expected though when using multipart requests the $body
variable remains empty.
public function postDebugAction(Request $request) {
$body = $request->getContent();
if (empty($body)) {
throw new \Exception('Body empty.');
}
return $this->view(array(), 201);
}
阅读此问答之后,我还添加了一个身体监听器.
After reading this question and answer I added a body listener aswell.
<?php
namespace VSmart\ApiBundle\Listener;
use FOS\RestBundle\EventListener\BodyListener as BaseBodyListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use FOS\RestBundle\Decoder\DecoderProviderInterface;
class BodyListener extends BaseBodyListener {
/**
* @var DecoderProviderInterface
*/
private $decoderProvider;
/**
* @param DecoderProviderInterface $decoderProvider Provider for fetching decoders
*/
public function __construct(DecoderProviderInterface $decoderProvider) {
$this->decoderProvider = $decoderProvider;
}
/**
* {@inheritdoc}
*/
public function onKernelRequest(GetResponseEvent $event) {
$request = $event->getRequest();
if (strpos($request->headers->get('Content-Type'), 'multipart/form-data') !== 0) {
return;
}
$format = 'json';
if (!$this->decoderProvider->supports($format)) {
return;
}
$decoder = $this->decoderProvider->getDecoder($format);
$iterator = $request->request->getIterator();
$request->request->set($iterator->key(), $decoder->decode($iterator->current(), $format));
}
}
根据我的PHPUnit测试,尽管使用Postman
和Advanced Rest Client
来模拟请求时,此方法仍然有效,但主体似乎又是空的.我仔细检查了一下,以使用调试器将两个模拟请求作为PHPUnit运行.结果是,实际上,通过Rest客户端进行仿真时,主体为空,而通过PHPUnit运行时,主体为空.
According to my PHPUnit test this was working though when using Postman
and Advanced Rest Client
to simulate the request the body seems to be empty again. I double checked this to run both the simulate requests as PHPUnit with the debugger. Result is that, indeed, the body is empty when simulated via a Rest client and not empty when ran through PHPUnit.
我使用的测试用例:
POST网址:
http://localhost/EntisServer/web/app_dev.php/api2/debug
标题:
Authorization: Bearer ZGYzYjY1YzY4MGY3YWM3OTFhYTI4Njk3ZmI0NmNmOWZmMjg5MDFkYzJmOWZkOWE4ZTkyYTRmMGM4NTE1MWM0Nw
Content-Type: multipart/form-data; boundary=-----XXXXX
内容:
-----XXXXX
Content-Disposition: form-data; name="json"
Content-Type: application/json; charset=utf-8
{
"blabla": 11
}
-----XXXXX
Content-Disposition: form-data; name="q_3101"; filename="image.jpg"
Content-Type: image/jpeg
contents of a file...
-----XXXXX--
更新
我不确定是否不使用BodyListener
单步调试器.当我这样做时,结果是完全一样的.因此,在没有BodyListener
的情况下,尽管模拟请求仍然为空,PHPUnit案例仍会获取主体.
UPDATE
I was uncertain whether I stepped through the debugger without using the BodyListener
. When I did the result is exactly the same. So, without the BodyListener
the PHPUnit case gets the body though the simulated request is still empty.
推荐答案
在fos_rest.decoder_provider
解码后,您可以在$request->files->all()
中找到上传的文件.
You can find your uploaded files in $request->files->all()
after fos_rest.decoder_provider
decoding.
这篇关于当多方请求时,Fosrestbundle主体为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!