Laravel 7 HTTP 客户端 - 无法使用“body”发送 POST 请求

Rad*_*ity 3 http request laravel guzzle laravel-7

使用 Laravel 7.6 及其内置的 HTTP 客户端。

body我正在尝试将原始 JSON 格式的简单 POST 请求发送到我的其他域,但没有成功:

$response = Http::post('https://example.com', [
    'body' => '{ test: 1 }'
]);
Run Code Online (Sandbox Code Playgroud)

我收到 400 Bad Request - 客户端错误 - 因为我的服务器期望body这是强制性的。

我在这里缺少什么?

小智 12

$response = Http:post('http://example.com/', [
   'foo' =>   'bar'
]);
Run Code Online (Sandbox Code Playgroud)

或这个

$response = Http::withHeaders([
 'User-Agent' => 'Some Agent',
])->asForm()->post('http://www.example.com/', [
  'foo' => 'bar',
]);
Run Code Online (Sandbox Code Playgroud)