【问题标题】:@Rest\Post 405- Method Not Allowed - FOSRestBundle@Rest\Post 405- 不允许的方法 - FOSRestBundle
【发布时间】:2014-04-05 16:08:39
【问题描述】:

我正在尝试使用 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';
}

}

非常感谢您。

【问题讨论】:

    标签: symfony doctrine-orm fosrestbundle


    【解决方案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
    

    【讨论】:

    • HTTP/1.1 201 已创建,感谢您的帮助@amineJallouli
    猜你喜欢
    • 2014-05-23
    • 2020-06-17
    • 2018-12-20
    • 2015-11-16
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    相关资源
    最近更新 更多