Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with JSON parsing #48

Closed
frankmichel opened this issue Sep 21, 2014 · 7 comments · Fixed by #52
Closed

Problem with JSON parsing #48

frankmichel opened this issue Sep 21, 2014 · 7 comments · Fixed by #52
Labels

Comments

@frankmichel
Copy link

A call to https://api.intercom.io/events returns a 202 response with an empty " " space in it. This causes issues with Guzzle since it cannot be parsed as json:

{"error":{"type":"Guzzle\Common\Exception\RuntimeException","message":"Unable to parse response body into JSON: 4","file":"/home/vagrant/myapp/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php","line":861}}

@nurazem
Copy link
Contributor

nurazem commented Sep 21, 2014

@josler Can you have a look at this? The response is indeed not a valid JSON.

@dehora
Copy link
Contributor

dehora commented Sep 21, 2014

Thanks @frankmichel. @nurazem @josler Ideally the server shouldn't return a whitespace artifact, but because the events resource doesn't return JSON the client shouldn't be looking for any on a 2xx response. Looks like the client will need a patch, marking as a bug.

@dehora dehora added the bug label Sep 21, 2014
@nurazem
Copy link
Contributor

nurazem commented Sep 21, 2014

Hi @dehora. I have looked into the issue and I think that the problem is on the server side. Although, the resource doesn't return JSON, the response's Content-Type header is set to 'application/json'. As you can see below Guzzle is trying to parse the body based on that header.
In Guzzle/Service/Command/DefaultResponseParser.php

public function parse(CommandInterface $command)
{
    $response = $command->getRequest()->getResponse();

    // Account for hard coded content-type values specified in service descriptions
    if ($contentType = $command['command.expects']) {
        $response->setHeader('Content-Type', $contentType);
    } else {
        $contentType = (string) $response->getHeader('Content-Type');
    }

    return $this->handleParsing($command, $response, $contentType);
}

protected function handleParsing(CommandInterface $command, Response $response, $contentType)
{
    $result = $response;
    if ($result->getBody()) {
        if (stripos($contentType, 'json') !== false) {
            $result = $result->json();
        } elseif (stripos($contentType, 'xml') !== false) {
            $result = $result->xml();
        }
    }

    return $result;
}

@aaronfessler
Copy link

I am experiencing this bug as well.

@webeng
Copy link

webeng commented Oct 6, 2014

Yep, me too.

@aaronfessler
Copy link

Hey @josler @nurazem what's the ETA on fixing this? It is pretty easy to repeat and is a showstopper:

use Intercom\IntercomBasicAuthClient;
$intercom = IntercomBasicAuthClient::factory(array(
    'app_id' => "XXXX",
    'api_key' => "YYYY"
));

$intercom->createEvent(array('user_id' => 1, 'created_at'=> time(),'event_name' => 'Code is bogus!'))

Yields this:

Fatal error: Uncaught exception 'Guzzle\Common\Exception\RuntimeException' with message 'Unable to parse response body into JSON: 4' in /Users/aaron/Sites/twine/lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php:861
Stack trace:
#0 /Users/aaron/Sites/twine/lib/vendor/guzzle/guzzle/src/Guzzle/Service/Command/DefaultResponseParser.php(47): Guzzle\Http\Message\Response->json()
#1 /Users/aaron/Sites/twine/lib/vendor/guzzle/guzzle/src/Guzzle/Service/Command/OperationResponseParser.php(80): Guzzle\Service\Command\DefaultResponseParser->handleParsing(Object(Guzzle\Service\Command\OperationCommand), Object(Guzzle\Http\Message\Response), 'application/jso...')
#2 /Users/aaron/Sites/twine/lib/vendor/guzzle/guzzle/src/Guzzle/Service/Command/DefaultResponseParser.php(39): Guzzle\Service\Command\OperationResponseParser->handleParsing(Object(Guzzle\Service\Command\OperationCommand), Object(Guzzle\Http\Message\Response), 'application/jso...')
#3 /Users/aaron/Sites/twine/lib/vendor/guzzle/guzzle/src/Guzzle/Service/Command/Opera in /Users/aaron/Sites/twine/lib/vendor/guzzle/guzzle/src/Guzzle/Http/Message/Response.php on line 861

Guzzle 4.2.3
Intercom 1.0.0-b3

@josler
Copy link
Contributor

josler commented Oct 7, 2014

Fixed with v1.0.0-b4!

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants