gpt4 book ai didi

php - 我如何在 Laravel 中发送 PUT 请求

转载 作者:行者123 更新时间:2023-12-04 07:28:41 25 4
gpt4 key购买 nike

我正在尝试通过向 ServiceDesk plus api 发送 http put 请求来更新数据。使用系统附带的控制台时,它运行良好,但是当我尝试从 Laravel 向同一个 api 发送请求时,它不起作用。
从下面的控制台请求
enter image description here
我正在尝试使用下面的代码向相同的 url 发送请求。

 private function openTicket($notification)
{
$data = json_encode(['input_data' => ['request' => ['subject' => $notification->subject,
'description' => $notification->description,
'status' => ['name' => 'Open']]]]);


$request_id = $notification->request_id;
$response = Http::withHeaders([
'technician_key' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX',
'Accept' => 'application/json'
])->put('http://localhost:8082/api/v3/requests/' . $request_id, $data);

dd($response);
}
我得到一个错误 400 错误请求 .
enter image description here

最佳答案

你不应该这样做 json_encode , laravel Http模块会自动为您完成。我认为您的数据现在是 json_encoded 两次。

$data = [
'input_data' => [
'request' => [
'subject' => $notification->subject,
'description' => $notification->description,
'status' => ['name' => 'Open']
]
]
]);


$request_id = $notification->request_id;
$response = Http::withHeaders([
'technician_key' => 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX',
'Accept' => 'application/json'
])->put('http://localhost:8082/api/v3/requests/' . $request_id, $data);

dd($response);

我刚注意到。根据您在屏幕截图中提供的文档, input_data数组中的嵌套级别不应该存在
$data = [
'request' => [
'subject' => $notification->subject,
'description' => $notification->description,
'status' => ['name' => 'Open']
]
]);

关于php - 我如何在 Laravel 中发送 PUT 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68081383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com