简体   繁体   English

对外部 API 的 POST 请求导致“500 内部服务器错误”

[英]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)所以我有一项任务需要使用 {{url}}/link-ec/submit 对外部 API 进行 POST(例如)

and i did with form request input, and end up with 500 Internal Server Error我做了表单请求输入,最终出现500 Internal Server Error

i'm using laravel controller for submitting this form我正在使用 laravel controller 提交此表格

this is my controller这是我的 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.问题是 API 无法反序列化请求正文。 Try to json encode the body before sending it, because the API expect a json in the body.在发送之前尝试对 json 进行编码,因为 API 期望在正文中包含 json。

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

    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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