我正在尝试获取API终结点的响应,但这会导致一个错误,表明这一点
服务器错误:
POST http://base_url/api/servicebookingrequest
导致500 Internal Server Error
响应:\ n {\“消息\”:\“一个错误发生。\“} \ n”,
我刚刚添加了到目前为止写的代码
try {
\Validator::make($req->all(), [
'chassis_no' => 'string',
'registration_no' => 'string',
'dealer_code' => 'string',
'pick_and_drop' => 'string',
'pick_address' => 'string',
'drop_address' => 'string',
'booking_date' => 'date',
'expected_service_date' => 'date',
'timeslots' => 'string',
]);
$client = new Client();
$res = $client->request(
'POST',
'http://bas_url/api/servicebookingrequest',
[
'headers' => [
'Content-Type' => 'application/json',
'_token' => csrf_token()
],
'form_params' => [
'ChassisNo' => $req->chassis_no,
'RegistrationNo' => $req->registration_no,
'DealerCode' => $req->dealer_code,
'PickAndDrop' => $req->pick_and_drop,
'PickAddress' => $req->pick_address,
'DropAddress' => $req->drop_address,
'BookingDate' => $req->booking_date,
'ExpectedServiceDate' => $req->expected_service_date,
'MultiSlotSelected' => $req->timeslot,
]
]
);
$res_body = $res->getBody()->getContents();
$res_body = json_decode($res_body, true);
response()->json(['status' => 'success', 'response' =>$res_body ]);
} catch (\GuzzleHttp\Exception\RequestException $e) {
response()->json(['status' => 'success', 'response' => $e->getMessage()]); ;
}
但是我在postman
中尝试了相同的API端点,它从服务器给出了正确的响应。
更新:-卷毛也可以正常工作。
非常感谢您解决此问题的任何帮助谢谢
curl
和postman
都使用相同的端点。该问题可能与您使用Guzzle
将请求发送到端点的方式有关。尝试将请求选项从form-params
更改为multipart/form-data。