狂饮Http 400错误请求

时间:2020-06-12 10:46:52

标签: php laravel guzzle

我想使用Guzzle Http来调用外部API。我的代码中buffer的请求使用了错误的结构。

这是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",
            ],
        ],
    ]
);

1 个答案:

答案 0 :(得分:-2)

替换

$client = new Client;

$client = new Client();