2016-04-12 80 views
-2

我的问题是,我的控制器像GET一样处理POST方法。当我尝试将参数传递给post方法时,它给了我GET结果,因为它们具有相同的语法。POST请求被认为是GET

我的POST功能如下:

/** 
* @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); 
} 

当我尝试发布使用这个网址的图片:http://localhost/test/web/app_dev.php/photos?tags[]=bebe&_format=json&image=E:\photos\n3ass.jpg,它让我得到结果。 如何解决这个问题?

回答

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

这是一个GET请求

阅读文档:http://www.w3schools.com/tags/ref_httpmethods.asp

如果你想模拟一个POST请求,您可以使用一些工具。在POST请求中,参数是正文而不是URL。

这个工具可以帮助你,如果你是不是在本地主机:https://www.hurl.it

在本地主机,我邀请您使用WebTestCase模拟本地POST请求http://symfony.com/doc/current/book/testing.html#working-with-the-test-client

+0

所以没有办法添加的参数在网址发布请求? – Nada

+0

没有 http://stackoverflow.com/questions/14551194/how-are-parameters-sent-in-an-http-post-request –

+0

好吧,我会尽量使用hurl.it – Nada