I am in the process of making a REST API, which will include both get-methods and post-methods. The get-methods work as planned, but when I use the same techniques for accessing parameters in post-methods, it doesn't work.
Here is an example of a post-controller (for testing purposes it doesn't actually do anything useful, but simply tries to access the name-parameter that I send):
/**
*
* Post a resource
*
* @ApiDoc(
* section="/resources"
* )
*
*
* @Rest\QueryParam(name="name", requirements="[a-z]+", default="Default name", description="Something")
*
*
* @param Request $request
* @return array
*
* @Rest\View()
* @param ParamFetcher $paramFetcher
*/
public function cpostAction(Request $request){
//$name = $paramFetcher->get('name');
$name = $request->get('name');
$testArr = array();
$testArr['name'] = $name;
return $testArr;
}
} // "api_post_resources" [post] /resources
Here is the request body:
Request Url: http://localhost:8080/utdanningsprosjekt/REST/Corsane/web/app_dev.php/api/resources.json
Request Method: POST
Status Code: 200
Params: {
"name": "test"
}
And here is what's returned:
[]
Some other approaches I've tried for accessing the parameter are:
- $name = $paramFetcher->get('name');
- $name = $request->query->get('name');
- $name = $request->request->get('name');
As mentioned, there have been no problems accessing the parameters when the method is a get method, but when it's a POST-request it doesn't seem to work. At some point I was able to make it work, but I don't know why, and was not able to reproduce it.
Do any of you know what the problem is or have any idea about what it might be or have to do with? Any help would be deeply appreciated!