laravel guzzlehttp-RequestException.php中的服务器异常 [英] laravel guzzlehttp - Server Exception in RequestException.php

查看:1032
本文介绍了laravel guzzlehttp-RequestException.php中的服务器异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我向我的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客户端发送发帖请求,它可以工作. 以下是跟踪

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

以下是我的控制器代码

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

有什么建议吗?

推荐答案

您说对了@ShaunBramley.我错过了在请求中发送内容类型标头的信息.添加以下代码有效-

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

这篇关于laravel guzzlehttp-RequestException.php中的服务器异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆