Skip to content

Commit

Permalink
yay json
Browse files Browse the repository at this point in the history
fixes #593
  • Loading branch information
Kenneth Reitz committed May 8, 2012
1 parent 7b6338c commit fe7348c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions requests/models.py
Expand Up @@ -7,6 +7,7 @@
This module contains the primary objects that power Requests.
"""

import json
import os
from datetime import datetime

Expand Down Expand Up @@ -796,8 +797,6 @@ def text(self):
if self.encoding is None:
if chardet is not None:
encoding = chardet.detect(self.content)['encoding']
else:
raise ValueError("Can't detect page encoding.")

# Decode unicode from given encoding.
try:
Expand All @@ -811,6 +810,14 @@ def text(self):

return content

@property
def json(self):
"""Returns the json-encoded content of a request, if any."""
try:
return json.loads(self.text or self.content)
except ValueError:
return None

def raise_for_status(self, allow_redirects=True):
"""Raises stored :class:`HTTPError` or :class:`URLError`, if one occurred."""

Expand Down

0 comments on commit fe7348c

Please sign in to comment.