Home > Software engineering >  POST Request to external API resulted in a `500 Internal Server Error`
POST Request to external API resulted in a `500 Internal Server Error`

Time:11-27

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

enter image description here

CodePudding user response:

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)]

    );
  • Related