我正在尝试使用fosrestBUndle启动我的REST API。我只是成功地运行了GET。但是当我输入此命令时,我遇到POST显示错误“405 Method not allowed”
wget -S localhost / tutonew / web / app_dev.php / api / test / add
Controller中的操作:
/**
* @Rest\View
* @Rest\Post("/api/test/add")
*
*/
public function postTestAction(Request $request) {
$entity = new Test();
$form = $this->createForm(new TestType(), $entity);
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirectView(
$this->generateUrl(
'get_test', array('id' => $entity->getId())
), Codes::HTTP_CREATED
);
}
return array(
'form' => $form,
);
}
名为的表格:
class TestType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('field', 'text');
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'Minn\AdsAPIBundle\Entity\Test',
'csrf_protection' => false
));
}
public function getName() {
return 'test';
}
}
非常感谢你。
答案 0 :(得分:1)
您正在使用GET方法,但在控制器中需要POST方法。
试试这个:
curl -i -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"test":{"field":"xxx"}}' http://localhost/tutonew/web/app_dev.php/api/test/add