Zennichimaro
Zennichimaro

Reputation: 5306

Symfony2 RestBundle unable to get POST param

I extends a FOSRestController and try to receive POST requests, but I'm unable to get the param, and all the requests to that API endpoint results in error:

Controller MapmsisdnController::postMapmsisdnAction() requires that you provide a value for the $deviceId argument (because there is no default value or because there is a non optional argument after this one). (500 Internal Server Error)

Here is my class and the annotation

class MapmsisdnController extends FOSRestController
{
    /**
     * Map a deviceid to msisdn
     * @ApiDoc(
     *  resource = true,
     *  description = "Map an msisdn for a single deviceid",
     *  statusCodes = {
     *      200 = "Returned when successful",
     *      500 = "Internal Server Error"
     *  }
     * )
     * @Post("/mapmsisdn")
     * @param type $deviceId
     * @param type $msisdn
     */
    public function postMapmsisdnAction($deviceId, $msisdn)
    {
        $test = true;
        if ( $test ) {
            $result = array(
                "code" => 200,
                "message" => "ok",
                "deviceId" => $deviceId,
                "msisdn" => $msisdn
            );
            $serializer = $this->container->get('jms_serializer');
            $ret = $serializer->serialize($result, 'json');
            return new Response($ret);
        }
    }
}

Upvotes: 1

Views: 1031

Answers (1)

Jenny Kim
Jenny Kim

Reputation: 1565

In the annotation, you should specify @Post, and specify your params one by one using @RequestParam like this:

@Post("/mapmsisdn")
@RequestParam(name="deviceId", requirements="\d+", default="", description="Device ID.")
@RequestParam(name="msisdn", requirements="\d+", default="", description="MSISDN.")

then you can retrieve the parameters like this:

$request = $this->getRequest();
$request->get('deviceId');
$request->get('msisdn');

@RequestParam is of this:

use FOS\RestBundle\Controller\Annotations\RequestParam;

Upvotes: 1

Related Questions