Laravel调用API返回null,Postman正常,请求体传递问题求助
解决Laravel调用API返回null的问题
问题排查与修复步骤
1. 补全缺失的请求字段
对比你的Postman请求体和Laravel代码,发现请求体缺少odi字段,这可能是API返回null的核心原因之一,先补上该字段:
$response8 = Http::POST($host->getserverIp() . '/PDFReport',[ "patientId"=>"001000010818", "reportId"=> "618", "con"=>"003001200326197", "departmentId"=> "128", "orderStatusId" => "12", "organizationId"=>"332", "sessionId"=> "3", "odi" => "2" // 补上Postman中存在的字段 ]);
2. 确保请求以JSON格式发送
Postman发送JSON请求时,请求头默认是Content-Type: application/json,但Laravel的Http::post默认发送的是表单编码数据(application/x-www-form-urlencoded)。如果目标API仅接受JSON格式的请求体,就会导致解析失败返回null,需要明确指定发送JSON:
方法一:使用asJson()快捷方法
$response8 = Http::asJson()->POST($host->getserverIp() . '/PDFReport',[ "patientId"=>"001000010818", "reportId"=> "618", "con"=>"003001200326197", "departmentId"=> "128", "orderStatusId" => "12", "organizationId"=>"332", "sessionId"=> "3", "odi" => "2" ]);
方法二:手动设置请求头
$response8 = Http::withHeaders([ 'Content-Type' => 'application/json', ])->POST($host->getserverIp() . '/PDFReport',[ "patientId"=>"001000010818", "reportId"=> "618", "con"=>"003001200326197", "departmentId"=> "128", "orderStatusId" => "12", "organizationId"=>"332", "sessionId"=> "3", "odi" => "2" ]);
3. 调试请求详情
如果仍返回null,打印请求的详细信息,确认请求是否正确发送:
dd($response8->status(), $response8->headers(), $response8->body());
通过状态码、响应头和原始响应内容,可进一步定位API返回null的具体原因。
内容的提问来源于stack exchange,提问作者user19704437
相关产品推荐
相关产品推荐

