Laravel 7 HTTP 客户端 - 无法发送带有“正文”的 POST 请求
Laravel 7 HTTP Client - Unable to send POST request with `body`
使用 Laravel 7.6 和它的内置 HTTP 客户端。
我正在尝试以 body
的原始 JSON 格式向我的其他域发送一个简单的 POST 请求,但没有成功:
$response = Http::post('https://example.com', [
'body' => '{ test: 1 }'
]);
我收到 400 Bad Request - Client error - 因为我的服务器期望 body
作为强制要求。
我在这里错过了什么?
$response = Http:post('http://example.com/', [
'foo' => 'bar'
]);
或者这个
$response = Http::withHeaders([
'User-Agent' => 'Some Agent',
])->asForm()->post('http://www.example.com/', [
'foo' => 'bar',
]);
试试这个,在我的情况下这应该有效。
$response = Http::withHeaders(['Content-Type' => 'application/json'])
->send('POST', 'https://example.com', [
'body' => '{ test: 1 }'
])->json();
使用 Laravel 7.6 和它的内置 HTTP 客户端。
我正在尝试以 body
的原始 JSON 格式向我的其他域发送一个简单的 POST 请求,但没有成功:
$response = Http::post('https://example.com', [
'body' => '{ test: 1 }'
]);
我收到 400 Bad Request - Client error - 因为我的服务器期望 body
作为强制要求。
我在这里错过了什么?
$response = Http:post('http://example.com/', [
'foo' => 'bar'
]);
或者这个
$response = Http::withHeaders([
'User-Agent' => 'Some Agent',
])->asForm()->post('http://www.example.com/', [
'foo' => 'bar',
]);
试试这个,在我的情况下这应该有效。
$response = Http::withHeaders(['Content-Type' => 'application/json'])
->send('POST', 'https://example.com', [
'body' => '{ test: 1 }'
])->json();