简体   繁体   中英

POST Request to external API resulted in a `500 Internal Server Error`

so i have a task that need to do POST to an external API with {{url}}/link-ec/submit (this is for example)

and i did with form request input, and end up with 500 Internal Server Error

i'm using laravel controller for submitting this form

this is my controller

$dataSubmit = [
                "customerName"          =>  $request->input('customerName'),
                "mobilePhone"           =>  $request->input('mobilePhone'),
                "nik"                   =>  $request->input('nik'),
                "birthPlace"            =>  $request->input('birthPlace'),
                "birthDate"             =>  $request->input('birthDate')
];



$clientSubmit = new \GuzzleHttp\Client(['headers' =>             
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer token']);

$responseSubmit = $clientSubmit->request(
            'POST',
            'url/link-ec/submit',
            ['json' => $dataSubmit]

        );
        $responseSubmit = json_decode($responseSubmit->getBody(), true);

        return $responseSubmit;

in case you guys need the route and form to know the problem:

Route::get('/post-link', 'IntegrationController@submit');
<form class="form form-fifastra financing-form" method="GET" id="form-apply"
                                action="{{ url('post-fifada') }}">
                                @csrf
</form>

this is the error

在此处输入图像描述

The problem is that the API cannot deserialize the request body. Try to json encode the body before sending it, because the API expect a json in the body.

$responseSubmit = $clientSubmit->request(
        'POST',
        'url/link-ec/submit',
        ['json' => json_encode($dataSubmit)]

    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM