枪口Http 400错误请求

问题描述 投票:1回答:1

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

这是json bodypostman,它很不错:

// 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.

php laravel guzzle
1个回答
0
投票

替换

$ client =新客户端;
$ client = new Client(); 
© www.soinside.com 2019 - 2024. All rights reserved.