请求带有JSON原始数据的主体逻辑

时间:2019-07-18 09:16:14

标签: php json symfony response symfony-3.4

我正在尝试通过邮递员发送原始JSON数据作为必需参数。

除了那部分,我的功能工作正常。当我将json数据粘贴到邮递员中时,它会抛出:

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;
}

并形成我的BaseRequest:

 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;
    }

1 个答案:

答案 0 :(得分:0)

找到了!

 $res = $client->request($method, $request->getUrl(), [
        'headers' => $headers,
        'json' => $request->getRequestParams()
    ]);