如何将 JSON 数据传递给 symfony2 中的 rest API
How to pass JSON data to rest API in symfony2
我正在为 symfony 2 创建 rest api,但无法将 JSON 数据传递给 post 方法。 $userform->isValid()
总是失败。
curl -v -H "Accept: application/json" -X POST -d '{"username":"hitesh","userpassword":"hitesh123"}' http://localhost/us/serenify/web/app_dev.php/user/login
这是我为测试目的传递的数据。
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;
}
以上是我的控制器代码。
当我打印请求值时,未以 JSON 格式显示。
要测试或传递 JSON 数据吗?我正在创建登录功能。
我最近需要这样的东西,因为我广泛使用 AngularJS
,其 $http
服务将数据作为 JSON
发送到我的控制器。
我通过实现监听传入请求的服务找到了解决方案,解压 JSON 并将其公开给 Request
对象。
检查 this link 的“正确方法”部分。
FOSRestBundle 就是为了这些目的而创建的。而且我认为你应该开始在你的项目中使用它。它没有开销并且易于使用。
https://github.com/FriendsOfSymfony/FOSRestBundle
此致。
我正在为 symfony 2 创建 rest api,但无法将 JSON 数据传递给 post 方法。 $userform->isValid()
总是失败。
curl -v -H "Accept: application/json" -X POST -d '{"username":"hitesh","userpassword":"hitesh123"}' http://localhost/us/serenify/web/app_dev.php/user/login
这是我为测试目的传递的数据。
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;
}
以上是我的控制器代码。
当我打印请求值时,未以 JSON 格式显示。
要测试或传递 JSON 数据吗?我正在创建登录功能。
我最近需要这样的东西,因为我广泛使用 AngularJS
,其 $http
服务将数据作为 JSON
发送到我的控制器。
我通过实现监听传入请求的服务找到了解决方案,解压 JSON 并将其公开给 Request
对象。
检查 this link 的“正确方法”部分。
FOSRestBundle 就是为了这些目的而创建的。而且我认为你应该开始在你的项目中使用它。它没有开销并且易于使用。
https://github.com/FriendsOfSymfony/FOSRestBundle
此致。