0

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.

1
  • Try $content = $request->getContent(); and you should get your string. Then you need to do $json = json_decode($content); Commented Jan 28, 2015 at 10:38

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

BigBoss is right - FOSRestBundle does do the trick in your case and you won't have to manually process each form submit. Here's a detailed tutorial on how to easily get started with FOSRest in Symfony
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.