[英]Guzzle Http 400 Bad Request
我想使用Guzzle Http來調用外部 API。 我在我的代碼中為請求的body
使用了錯誤的結構。
這是來自postman
的json body
,工作正常:
// 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 = new Client;和
$client = 新客戶();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.