kennethreitz/requests — Issue #397: Ability to declare how the content should be parsed
Author: timClicks
Created: 2012-01-29 03:39:39+00:00
State: open

Just an idea. Make it super-simple to get the objects to users. When playing in the Python prompt with a new API, this looks pretty normal:

```python
>>> import json
>>> import requests
>>> res = requests.get('http://example.com/data.json')
>>> data = json.loads(res.content)
...
```

Here's a suggestion that might add a little convenience for this kind of explorative work:

```python
>>> import requests
>>> res = requests.get("http://example.com/data.json", as='json')
>>> data = res.content
...
```
The idea is, dynamically import a parser. There are reasonable defaults in the stdilb. It will only save a few lines of code, but would make it even easier to play around with APIs in the Python prompt. I thought the `as` option could accept `xml`, `json`, `text`, `csv`, `tsv` and `infer`. `infer` would use an appropriate the `Content-Type` and/or the file extension and/or `format=xml` in the URL to make the choice/

Possibly a more sensible way to approach this kind of thing would be to specify a callback:

```python
requests.get("http://example.com/data.json", parse_content_with=json.loads)
```