无法在 Laravel 中复制 cURL 请求
Can't replicate cURL request in Laravel
我可以使用 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's documentation 之后):
$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 的内容以进行比较,但是即使 withOptions(['debug' => true])
存在,Laravel 也没有向我显示有效负载。
$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' => [access-key ...]
我可以使用 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's documentation 之后):
$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 的内容以进行比较,但是即使 withOptions(['debug' => true])
存在,Laravel 也没有向我显示有效负载。
$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' => [access-key ...]