Guzzel异常-500内部服务器错误

问题描述 投票:0回答:1

我正在尝试获取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端点,它从服务器给出了正确的响应。

更新:-卷毛也可以正常工作。

非常感谢您解决此问题的任何帮助谢谢

php laravel api curl guzzle
1个回答
0
投票
该错误与服务器端代码有关,因为curlpostman都使用相同的端点。该问题可能与您使用Guzzle将请求发送到端点的方式有关。尝试将请求选项从form-params更改为multipart/form-data
© www.soinside.com 2019 - 2024. All rights reserved.