laravel guzzlehttp - Server Exception in RequestException.php

你离开我真会死。 提交于 2019-12-25 07:06:40

问题


I am getting ServerException in RequestException.php line 107 when I make POST request to my API. I get following error - Server error: POST http://10.10.1.40:3000/auth/register resulted in a 500 Internal Server Error response: {"statusCode":500,"errorMessage":"[object Object]"}. I tried sending post request from REST Client and it works. Following is the trace

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))

Following is my controller code

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();   
    } 
}   

Any suggestions?


回答1:


You are right @ShaunBramley. I missed out sending the content type header in request. Adding following code works -

$headers = ['Content-Type' => 'application/json'];
$res = $client->request('POST', 'http://10.10.1.40:3000/auth/register', ['headers'=>$headers,'body' => $jsonData]);


来源:https://stackoverflow.com/questions/35342268/laravel-guzzlehttp-server-exception-in-requestexception-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!