在Symfony中接收POST数据

时间:2016-10-29 06:12:41

标签: php symfony

我是Symfony 3的初学者。

我收到发布数据时遇到问题。

我的控制器包含一个动作" test":

use FOS\RestBundle\Request\ParamFetcher;
use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\RequestParam

....

/**
 * @Post("/test")
 * @RequestParam(name="test")
 */
public function testAction(ParamFetcher $paramFetcher)
{
    var_dump($paramFetcher->get('test'));
    var_dump('the end');
}

当我发送请求时(我从开发人员的Chrome工具中复制粘贴):

General:
Request URL:http://service-user.local/app_dev.php/test
Request Method:POST
Status Code:400 Bad Request
Remote Address:127.0.0.1:80

Response Headers
view source
Cache-Control:no-cache
Connection:keep-alive
Content-Type:application/json
Date:Sat, 29 Oct 2016 06:06:05 GMT
Server:nginx/1.11.5
Transfer-Encoding:chunked
X-Debug-Token:1021bb
X-Debug-Token-Link:http://service-user.local/app_dev.php/_profiler/1021bb
X-Powered-By:PHP/7.0.11

Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate, lzma
Accept-Language:pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:9
Content-Type:text/plain;charset=UTF-8
Host:service-user.local
Origin:chrome-extension://kajfghlhfkcocafkcjlajldicbikpgnp
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 OPR/40.0.2308.81

Request Payload
test=test

我收到回复:

{"error":{"code":400,"message":"Bad Request","exception":[{"message":"Parameter \"test\" of value \"NULL\" violated a constraint \"This value should not be null.\"","class":"FOS\\RestBundle\\Exception\\InvalidParameterException","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":"\/code\/vendor\/friendsofsymfony\/rest-bundle\/Exception\/InvalidParameterException.php","line":68,"args":[]},{"namespace":"FOS\\RestBundle\\Exception","short_class":"InvalidParameterException","class":"FOS\\RestBundle\\Exception\\InvalidParameterException","type":"::","function":"withViolationsAndMessage","file":"\/code\/vendor\/friendsofsymfony\/rest-bundle\/Exception\/InvalidParameterException.php","line":52,"args":[["object","FOS\\RestBundle\\Controller\\Annotations\\RequestParam"],["object","Symfony\\Component\\Validator\\ConstraintViolationList"],["string","Parameter \"test\" of value \"NULL\" violated a constraint \"This value should not be null.\""]]},{"namespace":"FOS\\RestBundle\\Exception","short_class":"InvalidParameterException","class":"FOS\\RestBundle\\Exception\\InvalidParameterException","type":"::","function":"withViolations","file":"\/code\/vendor\/friendsofsymfony\/rest-bundle\/Request\/ParamFetcher.php","line":162,"args":[["object","FOS\\RestBundle\\Controller\\Annotations\\RequestParam"],["object","Symfony\\Component\\Validator\\ConstraintViolationList"]]},{"namespace":"FOS\\RestBundle\\Request","short_class":"ParamFetcher","class":"FOS\\RestBundle\\Request\\ParamFetcher","type":"->","function":"cleanParamWithRequirements","file":"\/code\/vendor\/friendsofsymfony\/rest-bundle\/Request\/ParamFetcher.php","line":108,"args":[["object","FOS\\RestBundle\\Controller\\Annotations\\RequestParam"],["null",null],["boolean",true]]},{"namespace":"FOS\\RestBundle\\Request","short_class":"ParamFetcher","class":"FOS\\RestBundle\\Request\\ParamFetcher","type":"->","function":"get","file":"\/code\/src\/AppBundle\/Controller\/DefaultController.php","line":24,"args":[["string","test"]]},{"namespace":"AppBundle\\Controller","short_class":"DefaultController","class":"AppBundle\\Controller\\DefaultController","type":"->","function":"testAction","file":null,"line":null,"args":[["object","FOS\\RestBundle\\Request\\ParamFetcher"]]},{"namespace":"","short_class":"","class":"","type":"","function":"call_user_func_array","file":"\/code\/vendor\/symfony\/symfony\/src\/Symfony\/Component\/HttpKernel\/HttpKernel.php","line":153,"args":[["array",[["object","AppBundle\\Controller\\DefaultController"],["string","testAction"]]],["array",[["object","FOS\\RestBundle\\Request\\ParamFetcher"]]]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"HttpKernel","class":"Symfony\\Component\\HttpKernel\\HttpKernel","type":"->","function":"handleRaw","file":"\/code\/vendor\/symfony\/symfony\/src\/Symfony\/Component\/HttpKernel\/HttpKernel.php","line":68,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"],["string","1"]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"HttpKernel","class":"Symfony\\Component\\HttpKernel\\HttpKernel","type":"->","function":"handle","file":"\/code\/vendor\/symfony\/symfony\/src\/Symfony\/Component\/HttpKernel\/Kernel.php","line":169,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"],["string","1"],["boolean",true]]},{"namespace":"Symfony\\Component\\HttpKernel","short_class":"Kernel","class":"Symfony\\Component\\HttpKernel\\Kernel","type":"->","function":"handle","file":"\/code\/web\/app_dev.php","line":30,"args":[["object","Symfony\\Component\\HttpFoundation\\Request"]]}]}]}}

有什么问题?我应该配置一些东西吗?

1 个答案:

答案 0 :(得分:1)

您的有效负载数据似乎不是正确的JSON?看起来应该更像这样:

{ "foo" : "bar", "name" : "John" }

如果使用jquery提交数据,则可以在表单上使用.serialize()函数。如果您将数据作为单个变量提交,只需定义如下消息:

{ "test" : "test" }

根据http://symfony.com/doc/current/bundles/FOSRestBundle/param_fetcher_listener.html的文档,如果它不喜欢你的参数,它将返回400错误,这就是你得到的。

相关问题