我想使用Guzzle Http来调用外部API。我的代码中请求的body
使用了错误的结构。
这是json body
的postman
,它很不错:
// json body that works fine on postman.
{
"field_1": "aaa",
"field_2": "bbb",
"field_3": [
{
"field_a": "ccc",
"field_b": "ddd",
}
]
}
这是我希望工作的equivalent code
:
use GuzzleHttp\Client;
$client = new Client;
$response = $client->request('POST',
"https://api.test.com/v1/some-endpoint",
[
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer some-access-token',
],
'json' => [
'field_1' => "aaa",
'field_2' => "bbb",
'field_3' => [
'field_a' => "ccc",
'field_b' => "ddd",
],
],
]
);
这导致此错误:
Exception trace:
1 GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response))
/var/www/my-project/vendor/guzzlehttp/guzzle/src/Middleware.php:65
2 GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
/var/www/my-project/vendor/guzzlehttp/promises/src/Promise.php:203
Please use the argument -v to see more details.
替换
$ client =新客户端;与
$ client = new Client();