douchou8935 2016-04-30 16:15
浏览 216
已采纳

返回GuzzleHttp响应对象会导致浏览器中出现ERR_INVALID_CHUNKED_ENCODING

I'm using guzzle 6 in laravel 5 to send a post request but I'm getting ERR_INVALID_CHUNKED_ENCODING when I try to access the request() in the method that handles the post request.

Here's my code:

Routes.php

Route::get('/guzzle', [
    'as'   => 'guzzle-test',
    'uses' => 'TestController@getTest'
]);

Route::post('/guzzle', [
   'as'   => 'guzzle-post-test',
   'uses' => 'TestController@postTest'
]);

TestController.php

public function getTest()
{
    $client = new Client();

    $data = [
        'hey' => 'ho'
    ];

    $request = $client->post(route('guzzle-post-test'), [
        'content-type' => 'application/json'
    ], json_encode($data));

    return $request;
}

public function postTest()
{
    dd(getTest());
}

I getting to the post request handler since I've tried to diedump a string and it gets there, but if i call the request() I get that error. For what I've researched It may have something to with the content length, but after reading guzzle's docs and some stuff around the web I could find how to get and pass the content length appropriately in the request. Any help would be very appreciated!

  • 写回答

1条回答 默认 最新

  • doufu1970 2016-07-18 07:27
    关注

    First off, here's some test code which you should be able to adapt for your purposes (also see form_params in the docs for GuzzleHttp):

    public function validateRecaptcha()
    {
        $client = new Client;
        $response = $client->request('POST', 'https://www.google.com/recaptcha/api/siteverify', [
                'form_params' => [
                    'secret' => env('RECAPTCHA_SECRET'),
                    'response' => Request::input('g-recaptcha-response'),
                    'remoteip' => Request::ip()
                ]
        ]);
    
        return $response;
    }
    

    I just ran into the same issue and found that trying to return the response object in Laravel gave me ERR_INVALID_CHUNKED_ENCODING. Whereas, doing a dd() on the response itself showed me what I was actually wanting to see:

    public function validateRecaptcha()
    {
        $client = new Client;
        $response = $client->request('POST', 'https://www.google.com/recaptcha/api/siteverify', [
                'form_params' => [
                    'secret' => env('RECAPTCHA_SECRET'),
                    'response' => Request::input('g-recaptcha-response'),
                    'remoteip' => Request::ip()
                ]
        ]);
    
        dd($response);
    }
    

    Unfortunately, without doing further research, I'm unable to explain why ERR_INVALID_CHUNKED_ENCODING keeps coming up when I try to return the client library's objects to the browser, but my initial inclination is that it's a data type issue.

    As far as your question goes, you're not actually trying to get back the "request" but rather the response. According to http://docs.guzzlephp.org/en/latest/quickstart.html#using-responses, if you want to get the API response contained in the response object (or at least in my case, I did), you'll want to use the getBody() method:

    public function validateRecaptcha()
    {
        $client = new Client;
        $response = $client->request('POST', 'https://www.google.com/recaptcha/api/siteverify', [
                'form_params' => [
                    'secret' => env('RECAPTCHA_SECRET'),
                    'response' => Request::input('g-recaptcha-response'),
                    'remoteip' => Request::ip()
                ]
        ]);
    
        return $response->getBody();
    }
    

    And then of course, if you expect it to be a JSON response (i.e. REST), then simply pass it to json_decode() to get your associative array back.

    public function validateRecaptcha()
    {
        $client = new Client;
        $response = $client->request('POST', 'https://www.google.com/recaptcha/api/siteverify', [
                'form_params' => [
                    'secret' => env('RECAPTCHA_SECRET'),
                    'response' => Request::input('g-recaptcha-response'),
                    'remoteip' => Request::ip()
                ]
        ]);
    
        return json_decode($response->getBody(), true); // true = assoc. array
    }
    

    Hope that helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码