无法在 Laravel 中复制 cURL 请求



我可以使用cURL发出请求,但是当我尝试使用Laravel复制它(返回404)时,它失败了。我已经确保发送的数据是相同的。

这是cURL:

curl --request POST 
--url https://www.api-provider.com/api/v2/access_keys 
--header 'Authorization: Bearer 53d9814ed1385f44e6d5ed24979e11c2f7da654b40' 
--header 'Content-Type: application/json' 
--data '{"access_key":{"key":"Lmy48Z0vA89","rate_ids":["[]"],"listing_id":"5de8f56797209ffbe","quantity":1,"is_active":true}}'

下面是对应的Laravel(遵循Guzzle的文档):

$response = Http::retry(3, 100)->withHeaders([
'Authorization' => 'Bearer 53d9814ed1385f44e6d5ed24979e11c2f7da654b40',
'Content-Type' => 'application/json'
])->post('https://www.api-provider.com/api/v2/access_keys',[
'json' => [
'access_key' => [
'key' => "Lmy48Z0vA89", 
'listing_id' => "5de8f56797209ffbe",
'rate_ids' => [
],
'quantity' => 1,
'is_active' => true,
]
],
]);

端点、id和授权令牌对于SO来说是假的,但假设它们是正确的,并且在cURL上工作,然后放到Laravel版本中。

给定的请求工作在cURL我假设错误是纯粹的语法?我试过调试,看看什么被发送到API比较,但Laravel没有显示我的有效载荷,即使withOptions(['debug' => true])在那里。

$response = Http::retry(3, 100)->withHeaders([
'Authorization' => 'Bearer 53d9814ed1385f44e6d5ed24979e11c2f7da654b40',
'Content-Type' => 'application/json'
])->post('https://www.api-provider.com/api/v2/access_keys',[
'access_key' => [
'key' => "Lmy48Z0vA89", 
'listing_id' => "5de8f56797209ffbe",
'rate_ids' => [
],
'quantity' => 1,
'is_active' => true,
]
]);

你真的需要绑定json数组'json' =>(访问密钥…]