【问题标题】:POST Request to external API resulted in a `500 Internal Server Error`对外部 API 的 POST 请求导致“500 内部服务器错误”
【发布时间】:2022-01-04 07:39:52
【问题描述】:

所以我有一个任务需要使用 {{url}}/link-ec/submit 对外部 API 进行 POST(例如)

我处理了表单请求输入,最后得到了500 Internal Server Error

我正在使用 laravel 控制器来提交此表单

这是我的控制器

$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;

如果你们需要路线和表格来了解问题:

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

这是错误

【问题讨论】:

  • in$responseSubmit 使用form_params 而不是json

标签: laravel api


【解决方案1】:

问题在于 API 无法反序列化请求正文。尝试在发送之前对正文进行 json 编码,因为 API 期望正文中有一个 json。

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

    );

【讨论】:

  • 我试过你的代码,但它返回这个“json_decode() 期望参数 1 是字符串,给定数组”
  • 看@ErandaDava,它现在是一个不同错误的不同错误,我们在我的代码 sn-p 中使用 json_encode,并且您在以下行中使用 json_decode 时遇到错误:$responseSubmit = json_decode( $responseSubmit->getBody(), true);
  • 好吧,我刚刚意识到这一点。感谢您的洞察力,我将解决此错误
  • 也许你可以使用:$responseSubmit = $responseSubmit->getBody(); (没有 json 解码)因为您的响应正文已经是一个数组
猜你喜欢
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多