opened
bczm8703
username_0
Hi.. i am using guzzle for my API post to a external server on my Laravel 4 application.
Under normal circumtances the external server will reply me with a JSON data and using response->json() will not cause any exception.

in event that the server experience technical issue like bad gateway, the response they provide me will not be json format data, thus the ParseException will happen.

i would like to use the getBody->read(65535) when this exception is called. but the response i got is empty.

My code:
```
$response = null;
try{
$response = $client->post('https://external.api.com/json_message', [
                'headers' => ['Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'],
                'body' => [
                    array('start_date' => '2015-07-14', 'end_date' => '2015-07-14');
                ],
               
               'stream' => true,
                'config' => [
                    'stream_context' => [
                        'ssl' => [
                                    'allow_self_signed' => true
                                ],            
                    ]
                ],
                'verify' => false
            ]);
$json_response = $response->json();

// some processing of data here

}
catch(\GuzzleHttp\Exception\ParseException $e) {
               if($response) {
$error_response = $response->getBody()->read(65535);

//$error_response is NULL
echo 'Response received is ' . $error_response . "\n";
             
            }

```
gracefully handling the ParseException
issue
closed
mtdowling
username_1
issue
created
mtdowling
username_1
You can't gracefully recover here when streaming data and not storing the data in a temporary buffer. Once a stream of data is consumed by attempting to parse the data, it is no longer possible to gracefully recover. Because you are attempting to parse the stream of data and loading the stream of data into memory anyways, I recommend that you store the response body in a variable and then JSON decode the data from the in memory string.
comment