Php 带有JSON原始数据的请求主体逻辑
我试图通过邮递员将原始JSON数据作为所需参数发送 除了那个部分,我的功能正常。当我将json数据粘贴到postman中时,它会抛出:Php 带有JSON原始数据的请求主体逻辑,php,json,symfony,response,symfony-3.4,Php,Json,Symfony,Response,Symfony 3.4,我试图通过邮递员将原始JSON数据作为所需参数发送 除了那个部分,我的功能正常。当我将json数据粘贴到postman中时,它会抛出: A non-empty request body is required. 我认为有一点修改需要,但我找不到解决办法 我的代码: public function callApi(BaseRequest $request) { $token = $this->getTokenForRequest($request); $method =
A non-empty request body is required.
我认为有一点修改需要,但我找不到解决办法
我的代码:
public function callApi(BaseRequest $request)
{
$token = $this->getTokenForRequest($request);
$method = $request->getMethod();
$client = new Client(['base_uri' => 'https://api-tst.testing.app/test/']);
$headers = [
'Authorization' => 'Bearer ' . $token,
'Ocp-Apim-Subscription-Key' => '1111112222',
'Content-Type' => 'application/json',
];
$request->getRequestParams();
$res = $client->request($method, $request->getUrl(), [
'headers' => $headers
]);
$res->getStatusCode();
$response = $res->getBody()->getContents();
return $response;
}
并形成我的基本要求:
public function getUrl()
{
return null;
}
public function getMethod()
{
return null;
}
/**
* @return array
*/
public function getRequestParams()
{
$data = [];
return $data;
}
我在控制器中调用它,如下所示:
public function testAll(Request $request)
{
$data = (string)$request->getContent();
$data = json_decode($data, true);
$response = $this->container->get('app')->myFunction($data);
dump($response);die;
}
找到了
$res = $client->request($method, $request->getUrl(), [
'headers' => $headers,
'json' => $request->getRequestParams()
]);