Question

I've been stuck here for quite a while but can't find anything helpful. I'm trying to connect to a website and get a response json file in Python3. The code looks like below:

conn = http.client.HTTPConnection('host.address')
params = "xx"+ xx + "xx" + ...
conn.request('GET', '/a/b/c', params)
resp = conn.getresponse()

This actually won't return the json file but the webpage http://host.address/a/b/c, which is an error page. However, while using the following code:

params = "xx"+ xx + "xx" + ...
resp = urllib.request.urlopen("http://host.address/a/b/c?"+params)

It returns correctly the json file. Any idea what's wrong with the code?

Thanks

Was it helpful?

Solution

In HTTP, only POST requests are supposed to have a body. The third parameter to request() is actually the body (see http://docs.python.org/py3k/library/http.client.html#http.client.HTTPConnection.request) - just build the URL as shown in the second example.

OTHER TIPS

Just to complement @sqrtsben's answer with an example:

import urllib.parse
import http.client

u = urllib.parse.urlparse("http://localhost:8080/index.php?utf8=✓")
conn = http.client.HTTPConnection(u.hostname, u.port)
if u.query == '':
    conn.request("GET", u.path)
else:
    conn.request("GET", u.path + '?' + u.query)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top