php - How to set proper JSON response for POST method in RESTful API from FOSRestBundle? -
i making post method restful api. api built on top of fosrestbundle , nelmioapidoc may notice. not able validate when file not uploaded or when rid parameter missing , response proper json. doing:
/** * set , upload avatar reps. * * @param paramfetcher $paramfetcher * @param request $request * * @apidoc( * resource = true, * https = true, * description = "set , upload avatar reps.", * statuscodes = { * 200 = "returned when successful", * 400 = "returned when errors" * } * ) * * @requestparam(name="rid", nullable=false, requirements="\d+", description="the id of representative") * @requestparam(name="avatar", nullable=false, description="the avatar file") * * @return view */ public function postrepsavataraction(paramfetcher $paramfetcher, request $request) { $view = view::create(); $uploadedfile = $request->files; // not working never error if not upload file if (empty($uploadedfile)) { $view->setdata(array('error' => 'invalid or missing parameter'))->setstatuscode(400); return $view; } $em = $this->getdoctrine()->getmanager(); $entreps = $em->getrepository('pdonebundle:representative')->find($paramfetcher->get('rid')); if (!$entreps) { $view->setdata(array('error' => 'object not found'))->setstatuscode(400); return $view; } .... code $repsdata = []; $view->setdata($repsdata)->setstatuscode(200); return $view; } if not upload file got response:
error: call member function move() on non-object 500 internal server error - fatalerrorexception but symfony exception error not json want , need, code never entering in if.
if not set rid got error:
request parameter "rid" empty 400 bad request - badrequesthttpexception but again symfony exception error , not json. how response proper json if rid not present or if file wasn't uploaded? advice?
$request->files instance of filebag. use $request->files->get('keyoffileinrequest') file.
rid specified required parameter, yeah, throws badrequesthttpexception if don't set it. behaves should. should try setting rid id isn't in database, should see own error message.
if want rid optional add default value rid:
* @requestparam(name="rid", nullable=false, requirements="\d+", default=0, description="the id of representative") something that. rid zero, repository::find call return null , error view returned. recommend keep is, proper behavior.
Comments
Post a Comment