2013-11-26 46 views
1

最近我在發佈到我的REST接口上時發現驗證錯誤。 Content-Type: application/x-www-form-urlencoded正在工作。使用Content-Type:multipart/form-data發佈結果爲空請求主體

這裏是我的測試功能:

/** 
* @Rest\Route("/testtype") 
*/ 
public function postTypeTestAction() 
{ 
    $request = $this->getRequest()->request->all(); 
    $phpContents = file_get_contents("php://input"); 

    return FOSView::create()->setStatusCode(200)->setData(array('request'=>$request, 'phpinput' => $phpContents)); 
} 

發帖時使用Content-Type: application/x-www-form-urlencoded

{ 
    "request":{ 
    "test":"message" 
    }, 
    "phpinput":"test=message" 
} 

發帖時使用Content-Type: multipart/form-data

{ 
    "request":[ 
    ], 
"phpinput":"------WebKitFormBoundaryFyQqAxqqfuhWzHUq\r\nContent-Disposition: form-data; name=\"test\"\r\n\r\nmessage\r\n------WebKitFormBoundaryFyQqAxqqfuhWzHUq--\r\n" 

}

由於沒有請求數據,我得到This value should not be blank驗證錯誤。這打破了我的申請。我一直盯着這麼久,我肯定我錯過了一些簡單的事情。

我使用Symfony 2.3.7和FOSRestBundle 1.0.0。

回答

1

這個問題在一夜之間就解決了。沒有服務器重新啓動,沒有更改代碼,並且我使用相同的工具來測試(Chrome擴展名爲DHC by Restlet)。但是,由於這不適用於開發環境和臨時環境,現在正在兩種環境中工作,這使得我唯一的答案就是測試工具,Chrome,本地緩存問題或某種組合。

獲得的經驗:使用多種測試工具。

現在,當我使用POST Content-Type: application/x-www-form-urlencoded

{ 
    "request":{ 
    "test":"message" 
    }, 
    "phpinput":"test=message" 
} 

現在,當我使用POST Content-Type: multipart/form-data

{ 
    "request":{ 
    "test":"message" 
    }, 
    "phpinput":"" 
}