httpful icon indicating copy to clipboard operation
httpful copied to clipboard

Assert for bad response before doing json parsing

Open aik099 opened this issue 11 years ago • 4 comments

If page is not there, then 404 code is returned and content isn't json. Then a parsing attempt causes exception to happen.

aik099 avatar Nov 09 '14 20:11 aik099

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.

nategood avatar Nov 13 '14 16:11 nategood

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 [).

aik099 avatar Nov 13 '14 17:11 aik099

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.

nategood avatar Nov 13 '14 17:11 nategood

Thanks.

aik099 avatar Nov 13 '14 18:11 aik099