Bob Dem
Bob Dem

Reputation: 1031

Python2 urllib/urllib2 wrong URL issue

I am coding a Python2 script to perform some automatic actions in a website. I'm using urllib/urllib2 to accomplish this task. It involves GET and POST requests, custom headers, etc.

I stumbled upon an issue which seems to be not mentioned in the documentation. Let's pretend we have the following valid url: https://stackoverflow.com/index.php?abc=def&fgh=jkl and we need to perform a POST request there.

How my code looks like (please ignore if you find any typo errors):

data = urllib.urlencode({ "data": "somedata", "moredata": "somemoredata" })
urllib2.urlopen(urllib2.Request("https://stackoverflow.com/index.php?abc=def&fgh=jkl", data))

No errors are shown, but according to the web server, the petition is being received to "https://stackoverflow.com/index.php" and not to "https://stackoverflow.com/index.php?abc=def&fgh=jkl". What is the problem here?

I know that I could use Requests, but I'd like to use urllib/urllib2 first.

Upvotes: 0

Views: 294

Answers (1)

Can Ibanoglu
Can Ibanoglu

Reputation: 604

If I'm not wrong, you should pass your request data in data dictionary you passed to the url open() function.

data = urllib.urlencode({'abc': 'def', 'fgh': 'jkl'})
urllib2.urlopen(urllib2.Request('http://stackoverflow.com/index.php'))

Also, just like you said, use Requests unless you absolutely need the low level access urllib provides.

Hope this helps.

Upvotes: 1

Related Questions