1

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.

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!

Tor
  • 254
  • 1
  • 7
  • 15

4 Answers4

1

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 :)

Tor
  • 254
  • 1
  • 7
  • 15
0

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

1ed
  • 3,668
  • 15
  • 25
  • Thanks for the reply! I've tried making it work by adding stuff about the body_listener in the config-file (which was previously not enabled). Here is the config.yml as it currently looks: http://pastebin.com/Uv3GxLiz. But accessing the data through $request->get('name') or $paramFetcher->get('name') will still not work. Again, I've not been able to make it work yet, but thanks a lot for the help - I will look into it more and read your link more carefully. – Tor May 13 '14 at 09:51
0

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

Community
  • 1
  • 1
Broncha
  • 3,794
  • 1
  • 24
  • 34
  • Thanks for the reply! I don't think that's the case though, as I've been able to return the json that I send in the POST-request (but not been able to parse it). – Tor May 13 '14 at 09:17
  • The following piece of code will return the json-body that I sent with the request (I don't fully understand how it all works): $params = json_decode($request->getContent(), true); return $params; – Tor May 13 '14 at 09:21
  • Ok, I get it now. @1ed is right about that. You are sending the request params in body, and those params are not available in the request object by default – Broncha May 13 '14 at 10:36
0
/**
 * @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;
}
Jorge Cuerdo
  • 103
  • 1
  • 4
Dmytro
  • 572
  • 9
  • 29
  • Thank you for the reply! The code as it looks there did not work for me "out of the box", but I've been able to use parts of it to make some progress, although I'm still not able to actually retrieve specific parameters. Trying to access parameters by calling upon $params[$paramName] will cause an error, even though the parameter asked for is sent in the request. Again - thanks for the help :) – Tor May 13 '14 at 09:28
  • it's my mistake. There was isset($params[$paramName]), i'm change function, but don't change parameters. Will correctly array_key_exists($paramName, $params). – Dmytro May 14 '14 at 10:15