Guzzle 请求:Post 正文数据

Guzzle Request : Post body data

我正在尝试 POST 数据到远程 AWS API。

数据应该是 body 部分的 JSON

使用 Postman,我可以发送数据并且一切正常:

现在,我正在尝试使用 GuzzleHttp\Psr7\Request 这样做:

$request = new \GuzzleHttp\Psr7\Request(
            'POST',
            'AWS API URL',
            ['Host' => 'AWS HOST', 'body' => '{"json": "my JSON"}']
        );
$request = $signer->signRequest($request, $credentials);
$response = $client->send($request);

请求成功,但没有数据更新!好像没有收到'body'

我无法访问远程 API 日志文件。

所以我的问题是,这是 post Guzzle 请求正文部分数据的正确方法吗?

谢谢。

根据链接的答案,您需要随请求传递以下选项:

[GuzzleHttp\RequestOptions::JSON => ['key1' => 'value1', 'key2' => 'val2']] 

或:

['json' => ['key1' => 'value1', 'key2' => 'val2']]

但是由于您需要先构建您的 Request 对象,因此您应该能够将此选项作为 Client::send 的第二个参数传递:

$response = $client->send($request, [
  GuzzleHttp\RequestOptions::JSON => ['key1' => 'value1', 'key2' => 'val2']
];