doudu8291
2016-10-29 06:12
浏览 90
已采纳

在Symfony中接收POST数据

I'a a beginner in Symfony 3.

I have a problem with receving post data.

My controller contains an action "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');
}

When I send a request (I copied-pasted it from Chrome tool for developers):

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

I get a response:

{"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"]]}]}]}}

What is the problem? Should I configure something?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongou6632 2016-10-30 09:09
    已采纳

    You payload data does not appear to be proper JSON? It should look more like this:

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

    If you are submitting the data with jquery you would use the .serialize() function on the form. If you are submitting the data as a single variable simply define the message like this:

    { "test" : "test" }
    

    According to the docs at http://symfony.com/doc/current/bundles/FOSRestBundle/param_fetcher_listener.html if it doesn't like your parameters it will return a 400 error, which is what you are getting.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题