当我向我的API发出POST请求时,我在RequestException.php第107行获取ServerException。我收到以下错误 -
服务器错误:POST http://10.10.1.40:3000/auth/register
导致500 Internal Server Error response:
{"statusCode":500,"errorMessage":"[object Object]"}
。
我尝试从REST客户端发送帖子请求,但它确实有效。
以下是跟踪
in RequestException.php line 107 at RequestException::create(object(Request), object(Response)) in Middleware.php line 65
at Middleware::GuzzleHttp\{closure}(object(Response)) in Promise.php line 199
at Promise::callHandler('1', object(Response), array(object(Promise), object(Closure), null)) in Promise.php line 152
at Promise::GuzzleHttp\Promise\{closure}() in TaskQueue.php line 60
at TaskQueue->run(true) in Promise.php line 240
at Client->request('POST', 'http://10.10.1.40:3000/auth/register', array('body' => '{"firstName":"abc","lastName":"ab"}')) in RegisterController.php line 30
at RegisterController->postRegisterForm(object(Request))
以下是我的控制器代码
class RegisterController extends Controller{
public function postRegisterForm(Request $request){
$jsonData = json_encode($_POST);
$client = new Client();
$res = $client->request('POST','10.10.1.40:3000/auth/register', ['body' => $jsonData]);
echo $res->getStatusCode();
echo $res->getBody();
}
}
有什么建议吗?
答案 0 :(得分:0)
你是对的@ShaunBramley。我错过了在请求中发送内容类型标头。添加以下代码有效 -
$headers = ['Content-Type' => 'application/json'];
$res = $client->request('POST', 'http://10.10.1.40:3000/auth/register', ['headers'=>$headers,'body' => $jsonData]);