Pass json objects as url parameters

Stumbled up this while trying to use the [Freebase API](http://wiki.freebase.com/wiki/API).

Basically, their API looks like that:

```
https://www.googleapis.com/freebase/v1/mqlread?query=...
```

as a query, they expect a JSON object, here's one that will [return a list of wines with their country and percentage of alcohol](http://www.freebase.com/queryeditor?q=%5B%7B%22country%22%3Anull%2C%22name%22%3Anull%2C%22percentage_alcohol%22%3Anull%2C%22percentage_alcohol%3E%22%3A0%2C%22type%22%3A%22%2Ffood%2Fwine%22%7D%5D):

```
[{
  "country":       null,
  "name":          null,
  "percentage_alcohol": null,
  "percentage_alcohol>": 0,
  "type":          "/food/wine"
}]​
```

Of course, we'll have to escape the hell out of this before passing it to an URL, so the actual query will look like this:

```
fullurl = 'https://www.googleapis.com/freebase/v1/mqlread?query=%5B%7B%22percentage_alcohol%3E%22%3A+0%2C+%22country%22%3A+null%2C+%22type%22%3A+%22%2Ffood%2Fwine%22%2C+%22name%22%3A+null%2C+%22percentage_alcohol%22%3A+null%7D%5D'
```

Now,

```
r = requests.get(fullurl)
print r.status_code
>>> 400
```

because the site claims it couldn't parse the query.

```
r2 = urllib2.urlopen(fullurl)
print r2.getcode()
>>> 200
```

No problem here, I get a proper return. Interestingly,

```
print urllib2.urlopen(r.url).getcode() 
>>> 200
```

Why? Am I doing something wrong?

On a related note, it would be nice if requests would parse dictionaries as JSON in `params`:

```
baseurl = "http://localhost/"
query =[{ 
    "name": "bill" ,
    "age": 42
}]
r = requests.get(baseurl, params={'query': query})
print r.url
>>> u'http://localhost/?query=age&query=name'
```

It seems as if it is just treating `query` as an iterable.

**EDIT**: 

```
r3 = urllib.urlopen(fullurl)
print r3.getcode()
>>> 400
```

Seems `urllib2` does something different than `urllib` there.


It's something weird with JSON copied from Freebase site:

```
import requests
full_url = 'https://www.googleapis.com/freebase/v1/mqlread?query=%5B%7B%22percentage_alcohol%3E%22%3A+0%2C+%22country%22%3A+null%2C+%22type%22%3A+%22%2Ffood%2Fwine%22%2C+%22name%22%3A+null%2C+%22percentage_alcohol%22%3A+null%7D%5D'
query = '[{"percentage_alcohol>": 0, "country": null, "type": "/food/wine", "name": null, "percentage_alcohol": null}]​'
print repr(query)
```

Outputs:

```
'...SKIPED... "percentage_alcohol": null}]\xe2\x80\x8b'
```

See this `\xe2\x80\x8b` at the end? So if I delete it, request is succeed:

```
query = query[:-3]
r1 = requests.get('https://www.googleapis.com/freebase/v1/mqlread', params={'query': query})
r2 = requests.get(full_url)
print r1.status_code # 200
print r2.status_code # 200
```

**Edit**

Maybe it's my editor/browser/os problem.


```
>>> fullurl = 'https://www.googleapis.com/freebase/v1/mqlread?query=%5B%7B%22percentage_alcohol%3E%22%3A+0%2C+%22country%22%3A+null%2C+%22type%22%3A+%22%2Ffood%2Fwine%22%2C+%22name%22%3A+null%2C+%22percentage_alcohol%22%3A+null%7D%5D'
>>> import requests
>>> requests.__version__
'0.12.0'
>>> r = requests.get(fullurl)
>>> print r.status_code
200
```

You might want to check your setup.


@reclosedev Interesting, but not the problem I'm having ;-)
@piotr-dobrogost Okay, I suppose it is my setup indeed:

```
>>> fullurl = 'https://www.googleapis.com/freebase/v1/mqlread?query=%5B%7B%22percentage_alcohol%3E%22%3A+0%2C+%22country%22%3A+null%2C+%22type%22%3A+%22%2Ffood%2Fwine%22%2C+%22name%22%3A+null%2C+%22percentage_alcohol%22%3A+null%7D%5D'
>>> import requests
>>> requests.__version__
'0.12.1'
>>> r = requests.get(fullurl)
>>> print r.status_code
400
```

I'm on Ubuntu 12.04, behind a proxy (which is known to do funny things), could that be the problem? If so, why does it work with `urllib2`, but not with `requests` and `urllib`?


It seems to work fine in my case (I am on Mac Snow Leopard). See the testcase: https://gist.github.com/2838282


@maebert 

I run it in Python3.5, seems your `]​` is special char.

```
>>> '[{"percentage_alcohol>": 0, "country": null, "type": "/food/wine", "name": null, "percentage_alcohol": null}]​'
'[{"percentage_alcohol>": 0, "country": null, "type": "/food/wine", "name": null, "percentage_alcohol": null}]\u200b'
>>> '[{"percentage_alcohol>": 0, "country": null, "type": "/food/wine", "name": null, "percentage_alcohol": null}]'
'[{"percentage_alcohol>": 0, "country": null, "type": "/food/wine", "name": null, "percentage_alcohol": null}]'
>>> 
```