如何将 post 参数更改为 json 原始格式
How to change post parameters to json raw format
我正在使用 FOSRestBundle 构建 Restful API。我有 POST Restful 服务的代码:
/**
* Create a new session.
* @param ParamFetcher $paramFetcher
*
* @ApiDoc(
* resource = true,
* https = true,
* description = "Create a new session",
* statusCodes = {
* 200 = "Returned when successful",
* 400 = "Returned when errors"
* }
* )
*
* @RequestParam(name="username", nullable=false, strict=true, description="The username")
* @RequestParam(name="veevaToken", nullable=false, strict=true, description="The token")
* @RequestParam(name="instanceUrl", nullable=false, strict=true, description="The instance URL")
*
* @return View
*/
public function postSessionAction(ParamFetcher $paramFetcher)
{
....
}
我将参数作为 x-www-form-urlencoded
发送,我想将其更改为原始格式,例如:
{
"veevaToken": "xxxxxx",
"instanceUrl":"xxxxxx",
"username":"xxxx"
}
我该怎么做?那可能吗?有什么建议吗?
POST 正常原始格式,但在您的 php 中而不是使用 $_POST[name] 使用
$http_raw = file_get_contents('php://input);
这将包含发布到服务器的原始数据,如果 json
,您将需要对其进行解码
我正在使用 FOSRestBundle 构建 Restful API。我有 POST Restful 服务的代码:
/**
* Create a new session.
* @param ParamFetcher $paramFetcher
*
* @ApiDoc(
* resource = true,
* https = true,
* description = "Create a new session",
* statusCodes = {
* 200 = "Returned when successful",
* 400 = "Returned when errors"
* }
* )
*
* @RequestParam(name="username", nullable=false, strict=true, description="The username")
* @RequestParam(name="veevaToken", nullable=false, strict=true, description="The token")
* @RequestParam(name="instanceUrl", nullable=false, strict=true, description="The instance URL")
*
* @return View
*/
public function postSessionAction(ParamFetcher $paramFetcher)
{
....
}
我将参数作为 x-www-form-urlencoded
发送,我想将其更改为原始格式,例如:
{
"veevaToken": "xxxxxx",
"instanceUrl":"xxxxxx",
"username":"xxxx"
}
我该怎么做?那可能吗?有什么建议吗?
POST 正常原始格式,但在您的 php 中而不是使用 $_POST[name] 使用
$http_raw = file_get_contents('php://input);
这将包含发布到服务器的原始数据,如果 json
,您将需要对其进行解码