Question

I am trying to start my REST API with fosrestBUndle. I just run the GET with success. But I met with the POST display the error "405 Method not allowed" when I put this command

wget -S localhost/tutonew/web/app_dev.php/api/test/add

The action in the 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,
    );
}

The Form that is called:

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';
}

}

Thank you very much in advance.

Was it helpful?

Solution

You are using a GET method but in your controller you need a POST method.

Try this:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top