-2

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?

Veve
  • 6,182
  • 5
  • 37
  • 53
anony
  • 185
  • 2
  • 13

1 Answers1

2
 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

François Dupont
  • 375
  • 3
  • 18