Assert for bad response before doing json parsing
If page is not there, then 404 code is returned and content isn't json. Then a parsing attempt causes exception to happen.
I wouldn't want to blindly remove json parsing when receiving a 40X. Many APIs out there will return useful json encoded errors along with 40X responses. Agree that this isn't always the case and having a option to "degrade on parse fail" when getting a 40X might be nice.
Currently I have disabled auto parsing and doing json_decode manually after I know response looks like JSON.
In fact you can check if { or [ is 1st symbol in the trimmed response to determine if it's at least closer to JSON then to HTML (I'm assuming that every JSON starts with { or [).
Checking the content type of the response is hopefully a good first test. I would start there when receiving a 40X error. Maybe by providing a "smart error" option.
Nate Good
http://nategood.com @nategood
On Thu, Nov 13, 2014 at 12:12 PM, Alexander Obuhovich < [email protected]> wrote:
Currently I have disabled auto parsing and doing json_decode manually after I know response looks like JSON.
In fact you can check if { or [ is 1st symbol in the trimmed response to determine if it's at least closer to JSON then to HTML (I'm assuming that every JSON starts with { or [).
— Reply to this email directly or view it on GitHub https://github.com/nategood/httpful/issues/155#issuecomment-62928960.
Thanks.