hitesh rane
hitesh rane

Reputation: 35

How to pass JSON data to rest API in symfony2

I am creating rest api for symfony 2,but not able to pass JSON data to post method. $userform->isValid()always failed.

curl -v -H "Accept: application/json" -X POST -d '{"username":"hitesh","userpassword":"hitesh123"}' http://localhost/us/serenify/web/app_dev.php/user/login

This is the data I am passing for test purpose.

  public function loginAction(Request $request)
    {
         return $this->processForm(new User(),$request);
    }



 private function processForm(User $user,$request)
    {   
        $userform = $this->createForm(new UserType(), $user);       
        $content = $this->getRequest();              
        $userform->submit($content);
       $key = md5(microtime().rand());
            if ($userform->isValid()) 
            {

                if(trim($data['username'])=="" || trim($data['userpassword']==""))
                { 
                       $data=array(
                                "success"=>'false',
                                "msg"=>'username or password is blank'    
                            );
                        $response = new Response(json_encode($data));
                        $response->setStatusCode(203);
                        $response->headers->set('Content-Type', 'application/json'); 
                }
                else
                {   
                    $username=trim($data['username']);
                    $userpassword=trim(md5($data['userpassword']));

                    $user = $this->getDoctrine()
                                       ->getRepository('SerenifyUserBundle:User')
                                       ->findOneBy(array('username' => $username, 'userpassword' => $userpassword));
                    if (!$user)
                    {
                        $data=array(
                                "success"=>'false',
                                "msg"=>'username or password is wrong'    
                            );
                        $response = new Response(json_encode($data));
                        $response->setStatusCode(404);
                        $response->headers->set('Content-Type', 'application/json');     
                    }
                    else
                    {
                         $data=array(
                                "success"=>'true',
                                "msg"=>'user has sucessfully logged in',
                                "username" => $username,
                                "sessionis" => $key,

                            );
                        $response = new Response(json_encode($data));
                        $response->setStatusCode(404);
                        $response->headers->set('Content-Type', 'application/json');  
                    }
                     $response = new Response(json_encode($data));    
                }
            }
            else
            {
                $data=array(
                                "success"=>'false',
                                "msg"=>'invalid form content'    
                            );
                        $response = new Response(json_encode($data));
                        $response->setStatusCode(404);
                        $response->headers->set('Content-Type', 'application/json');  

             }
        return $response;
    }

Above is my controller code.

When I print request value is does not show in JSON format.

Anyway to test or pass JSON data? I am creating login functionality.

Upvotes: 0

Views: 2283

Answers (2)

BigBoss
BigBoss

Reputation: 61

FOSRestBundle was created for these purposes. And I think you should start to use it in your project. It has no overhead and is easy to use.

https://github.com/FriendsOfSymfony/FOSRestBundle

Regards.

Upvotes: 1

Jovan Perovic
Jovan Perovic

Reputation: 20193

I recently have had a need to something like this as I am extensively using AngularJS whose $http service sends data as JSON to my controllers.

I found a solution by implementing service which listens to incoming requests unpacks JSON and exposes it to Request object.

Check "The right way" section of this link.

Upvotes: 1

Related Questions