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

Handle 204 and zero length content gracefully #30

Closed
ghost opened this issue May 16, 2012 · 2 comments
Closed

Handle 204 and zero length content gracefully #30

ghost opened this issue May 16, 2012 · 2 comments
Assignees
Labels
Milestone

Comments

@ghost
Copy link

ghost commented May 16, 2012

I'm working with an REST API that responds to a successful DELETE with a 204 and no body (it clearly states this in the response headers with Content-Length: 0)

I found that httpful still tried to parse the response as JSON and falls over in a heap:

Fatal error: Uncaught exception 'Exception' with message 'Unable to parse response as JSON' in phar://C:/Users/colemanb/Documents/Cloud/hpcloud/PHP/lib/httpful.phar/Httpful/Handlers/JsonHandler.php:19
Stack trace:
#0 phar://C:/Users/colemanb/Documents/Cloud/hpcloud/PHP/lib/httpful.phar/Httpful/Response.php(92): Httpful\Handlers\JsonHandler->parse('')
#1 phar://C:/Users/colemanb/Documents/Cloud/hpcloud/PHP/lib/httpful.phar/Httpful/Response.php(41): Httpful\Response->_parse('')
#2 phar://C:/Users/colemanb/Documents/Cloud/hpcloud/PHP/lib/httpful.phar/Httpful/Request.php(174): Httpful\Response->__construct('', 'HTTP/1.1 204 No...', Object(Httpful\Request))
#3 C:\Users\colemanb\Documents\Cloud\hpcloud\PHP\hpcloud-api.php(107): Httpful\Request->send()
#4 C:\Users\colemanb\Documents\Cloud\hpcloud\PHP\demo.php(82): api_call_delete('HPAuth_4fb362aa...', 'https://az-2.re...', '41792484084457', 'servers/123657')
#5 {main}  thrown in phar://C:/Users/colemanb/Documents/Cloud/hpcloud/PHP/lib/httpful.phar/Httpful/Handlers/JsonHandler.php on line 19

I got round this by calling autoParse(false) however should the API call error in some way there might be content in the response body that I want to handle. Shouldn't the Response be checked to ensure it's not zero length before invoking the parser? This would make sense to me!

@nategood
Copy link
Owner

Yep it should not try to parse empty bodies. Thanks for the report.

@ghost
Copy link
Author

ghost commented May 16, 2012

No worries. This is a really great library! No more will I battle with with 47 lines of curl_setopt just to make a HTTP call! :)

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

No branches or pull requests

1 participant