I try to perform a simple POST-request with urllib2.
However the servers response indicates that it receives a simple GET. I checked the type of the outgoing request, but it is set to POST.
To check whether the server behaves like I expect it to, I tried to perform a GET request with the (former POST-) data concatenated to the url. This got me the answer I expected.
Does anybody have a clue what I misunderstood?
def connect(self):
url = 'http://www.mitfahrgelegenheit.de/mitfahrzentrale/Dresden/Potsdam.html/'
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
header = { 'User-Agent' : user_agent }
values = {
'city_from' : 69,
'radius_from' : 0,
'city_to' : 263,
'radius_to' : 0,
'date' : 'date',
'day' : 5,
'month' : 03,
'year' : 2012,
'tolerance' : 0
}
data = urllib.urlencode(values)
# req = urllib2.Request(url+data, None, header) # GET works fine
req = urllib2.Request(url, data, header) # POST request doesn't not work
self.response = urllib2.urlopen(req)
This seems to be a problem like the one discussed here: Python URLLib / URLLib2 POST but I'm quite sure that in my case the trailing slash is not missing. ;)
I fear this might be a stupid misconception, but I'm already wondering for hours!
EDIT: A convenience function for printing:
def response_to_str(response):
return response.read()
def dump_response_to_file(response):
f = open('dump.html','w')
f.write(response_to_str(response))
EDIT 2: Resolution:
I found a tool to capture the real interaction with the site, http://fiddler2.com/fiddler2/. Apparently the server takes the data from the input form, redirects a few times and and then makes a GET request with this data simply appended to the url.
Everything is fine with urllib2 and I apologize for misusing your time!
POST
requests to this page then. – Tree