Laravel 7对外部API响应::setContent()的类型必须为字符串或null

q3aa0525  于 2023-01-21  发布在  其他
关注(0)|答案(2)|浏览(102)

我有问题的HTTP API后请求使用laravel 7我尝试与 Postman 是工作正常
验证码:

$response = Http::withOptions([
                    'debug' => true,
                ])->asForm()->withToken('mybearertoken')->post('theurl',[
                    'tracking' => $getcolisdetails->ref_order,
                    'state_id' => $newStatus ,
                    'content'  => $content
                                ]);
                                $response->json() ;

我得到的回答是:

"Argument 1 passed to Symfony\\Component\\HttpFoundation\\Response::setContent() must be of the type string or null, object given

注意:我使用的是laravel 7,PHP 7.4,guzzlehttp/guzzle”:“^6.3

gdrx4gfi

gdrx4gfi1#

获取字符串形式的JSON内容,对其进行解码,并将其设置为响应内容。

$response = Http::withOptions([
    'debug' => true,
])->asForm()->withToken('mybearertoken')->post('theurl',[
    'tracking' => $getcolisdetails->ref_order,
    'state_id' => $newStatus ,
    'content'  => $content
]);

$content = $response->getContent();
response(json_decode($content), 200)->header('Content-Type', 'application/json');
xtfmy6hx

xtfmy6hx2#

我用guzle找到的解决方案是:

try {
    $response = (new Client([
      'debug' => true,
    ]))->post('theurl', [
      'headers' => [
          'Authorization' => 'Bearer mybearertoken',
      ],
      'json' => [
          'tracking' => $getcolisdetails->ref_order,
          'state_id' => $newStatus,
          'content'  => $content
      ],
    ]);
    $data = $response->getBody()->getContents();
  } catch (ClientException $exception) {
    dd($exception->getResponse()->getBody()->getContents());
  }

相关问题