Tor
Tor

Reputation: 284

Unable to access parameters from POST request in Symfony

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:

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.

Screenshot from the REST Console Chrome-extension, that I use to test the requests

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!

Upvotes: 1

Views: 7646

Answers (4)

Dmytro
Dmytro

Reputation: 582

/**
 * @param string $paramName
 * @return mixed
 */
protected function _getParam($paramName)
{
    $param = $this->getRequest()->request->get($paramName);
    if (!$param) {
        $params = json_decode($this->getRequest()->getContent(), true);
        if (array_key_exists($paramName, $params)) {
            $param = $params[$paramName];
        }
    }

    return $param;
}

Upvotes: 0

Broncha
Broncha

Reputation: 3794

May be it has something to do with this No POST data received Symfony

Your GET requests are working because when you are being redirected the query params are being passed along, but the data are not passed along for POST requests.

You might just have a bad rewrite somewhere. You could track that with follow redirection

Upvotes: 0

1ed
1ed

Reputation: 3668

If you want to access json data from the request you should enable body listeners.

Upvotes: 0

Tor
Tor

Reputation: 284

It started working when I switched to using either x-www-form-urlencoded format or form-data format (and from the REST Console chrome extension to the Postman - REST Client chrome extension, although that might not have anything to do with it).

So the following request works (x-www-form-urlencoded format):

POST /utdanningsprosjekt/Corsane/web/app_dev.php/api/resources.json HTTP/1.1 Host: localhost:8080 Cache-Control: no-cache Content-Type: application/x-www-form-urlencoded

name=test&url=www.test.com

and this one (form-data format) works as well:

POST /utdanningsprosjekt/Corsane/web/app_dev.php/api/resources.json HTTP/1.1 Host: localhost:8080 Cache-Control: no-cache

----WebKitFormBoundaryE19zNvXGzXaLvS5C Content-Disposition: form-data; name="name"

tor ----WebKitFormBoundaryE19zNvXGzXaLvS5C Content-Disposition: form-data; name="url"

www.hey.com ----WebKitFormBoundaryE19zNvXGzXaLvS5C

while this doesn't work (raw format):

POST /utdanningsprosjekt/Corsane/web/app_dev.php/api/resources.json HTTP/1.1 Host: localhost:8080 Cache-Control: no-cache

[{"name":"tor"}]

As JSON is the prefered format I will look into finding out how I can handle json-formated POST-requests server-side, and would be thankful for any input in regards to this issue, but at least I am no longer feel helpless when it comes to posting data through the REST API :)

Upvotes: 1

Related Questions