Search code examples
symfonyfosrestbundle

POST request is considered as GET


My problem is that my controller handles the POST method like GET. When I try to pass arguments to a post method it gives me the GET results because they have the same syntax.

My POST function is as follows:

/**
 * @ApiDoc(description="Uploads photo with tags.")
 *
 * @Rest\FileParam(name="image", image=true, description="Image to upload.")
 * @Rest\RequestParam(name="tags", requirements=".+", nullable=true, map=true, description="Tags that associates photo.")
 * @Rest\View()
 */
public function postPhotoAction(ParamFetcher $paramFetcher, array $tags)
{
    $em = $this->getDoctrine()->getManager();

    $photo = new Photo();
    $form = $this->createForm(new PhotoType, $photo);

    if ($tags) {
        $tags = $em->getRepository('TestTaskTagsBundle:Tag')->findOrCreateByTitles($tags);
    }

    $form->submit($paramFetcher->all());

    if (!$form->isValid()) {
        return $form->getErrors();
    }

    foreach ($tags as $tag) {
        $photo->addTag($tag);
    }

    $em->persist($photo);
    $em->flush();

    return array('photo' => $photo);
}

When I try to post an image using this url : http://localhost/test/web/app_dev.php/photos?tags[]=bebe&_format=json&image=E:\photos\n3ass.jpg, it shows me GET results. How to fix that?


Solution

  •  http://localhost/test/web/app_dev.php/photos?tags[]=bebe&_format=json&image=E:\photos\n3ass.jpg
    

    This is a GET request

    Read docs : http://www.w3schools.com/tags/ref_httpmethods.asp

    If you want simulate a POST request you can use some tools. In a POST request, parameters are in body and not in URL.

    This tools can help you if you are not in localhost : https://www.hurl.it

    In localhost, I invite you to use WebTestCase to simulate local POST request http://symfony.com/doc/current/book/testing.html#working-with-the-test-client