phprestsymfonyfosrestbundlesymfony-2.6

How to change post parameters to json raw format


I am using FOSRestBundle for build a Restful API. I have this code for a POST Restful service:

/**
 * Create a new session.
 * @param ParamFetcher $paramFetcher
 *
 * @ApiDoc(
 *   resource = true,
 *   https = true,
 *   description = "Create a new session",
 *   statusCodes = {
 *      200 = "Returned when successful",
 *      400 = "Returned when errors"
 *   }
 * )
 *
 * @RequestParam(name="username", nullable=false, strict=true, description="The username")
 * @RequestParam(name="veevaToken", nullable=false, strict=true, description="The token")
 * @RequestParam(name="instanceUrl", nullable=false, strict=true, description="The instance URL")
 *
 * @return View
 */
public function postSessionAction(ParamFetcher $paramFetcher)
{
     ....
}

I send parameters as x-www-form-urlencoded by I want to change that to a raw format as for example:

{
  "veevaToken": "xxxxxx",
  "instanceUrl":"xxxxxx",
  "username":"xxxx"
}

How I do that? Is that possible? Any advice?


Solution

  • POST Normal raw format but in your php instead of using $_POST[name] use

       $http_raw = file_get_contents('php://input);
    

    And that will contain the raw data which is being posted to the server that you will need to decode if its json