Connecting Tech Pros Worldwide Forums | Help | Site Map

Form Value Won't Post/Submit

SuperMetroid's Avatar
Newbie
 
Join Date: Oct 2009
Posts: 5
#1: Oct 12 '09
The html code of the form, and my code are below. I can't get the value to post/submit.. instead I get an error. Can anyone help?

HTML Code of Form:
Expand|Select|Wrap|Line Numbers
  1. <form method='post' autocomplete='off'>
  2.     <input type='hidden' name='action' value='grant-revoke' />
  3.     <input type='hidden' name='creator_badge_index' value='1' />
  4.  
  5.     <input type='hidden' name='token' value='92dcd92a8bc16f73f330d118ae1ed891' />
  6.     <input type='hidden' name='do-grant' value='1' />
  7.     <div id='grant-div'><span class='label'>Grant badge: </span><input type='text' id='grant-userid' name='grant-userid' value='userid / avatar name' /><input type='submit' value='Grant!' /></div>
  8. </form>
My Code:
Expand|Select|Wrap|Line Numbers
  1. opener = urllib.request.build_opener()
  2. cj = http.cookiejar.MozillaCookieJar()
  3. cj.load('C:/Users/Alison/Documents/moz_cookies.txt')
  4. opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
  5.  
  6. params = urllib.parse.urlencode({'grant-userid' : 'Guest_xLolKittyx'})
  7. form = urllib.request.OpenerDirector.open('http://www.imvu.com/catalog/web_manage_badges.php?action=grant-revoke&creator_badge_index=1', params)
  8. data = form.read()
  9. form.close()
  10. print(data)
Error Message:
Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "C:\Python31\htmlparser.py", line 34, in <module>
  3.     form = urllib.request.OpenerDirector.open('http://www.imvu.com/catalog/web_manage_badges.php?action=grant-revoke&creator_badge_index=1', params)
  4.   File "C:\Python31\lib\urllib\request.py", line 332, in open
  5.     req = Request(fullurl, data)
  6.   File "C:\Python31\lib\urllib\request.py", line 174, in __init__
  7.     self._parse()
  8.   File "C:\Python31\lib\urllib\request.py", line 179, in _parse
  9.     raise ValueError("unknown url type: %s" % self.full_url)
  10. ValueError: unknown url type: grant-userid=Guest_xLolKittyx

Newbie
 
Join Date: Oct 2009
Posts: 2
#2: Oct 13 '09

re: Form Value Won't Post/Submit


You are mixing GET-type (indicated by ? in the URL) and POST-type
parameters. Put the action and creator_badge_index parameters also in
the dictionary. And probably you need to provide the other hidden fields
from the form also.

Something like (untested):

Expand|Select|Wrap|Line Numbers
  1. paramdict = {
  2.           'action': 'grant-revoke',
  3.           'creator_badge_index': '1',
  4.           'token': '92dcd92a8bc16f73f330d118ae1ed891',
  5.           'do-grant': '1',
  6.           'grant-userid' : 'Guest_xLolKittyx',
  7.           }
  8. params = urllib.parse.urlencode(paramdict)
  9. url = 'http://www.imvu.com/catalog/web_manage_badges.php'
  10. form = urllib.request.OpenerDirector.open(url, params)
SuperMetroid's Avatar
Newbie
 
Join Date: Oct 2009
Posts: 5
#3: Oct 13 '09

re: Form Value Won't Post/Submit


Thanks for the response! I changed the code, but I still get an error when it is run.. :/

Error Message:
Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "C:\Python31\htmlparser.py", line 40, in <module>
  3.     form = urllib.request.OpenerDirector.open('http://www.imvu.com/catalog/web_manage_badges.php', params)
  4.   File "C:\Python31\lib\urllib\request.py", line 332, in open
  5.     req = Request(fullurl, data)
  6.   File "C:\Python31\lib\urllib\request.py", line 174, in __init__
  7.     self._parse()
  8.   File "C:\Python31\lib\urllib\request.py", line 179, in _parse
  9.     raise ValueError("unknown url type: %s" % self.full_url)
  10. ValueError: unknown url type: action=grant-revoke&creator_badge_index=1&token=92dcd92a8bc16f73f330d118ae1ed891&do-grant=1&grant-userid=Guest_xLolKittyx
  11.  
Oh, and I tried it without the hidden fields as well, but I still get an error.
Newbie
 
Join Date: Oct 2009
Posts: 2
#4: Oct 13 '09

re: Form Value Won't Post/Submit


OK, there is another problem that I overlooked.
Expand|Select|Wrap|Line Numbers
  1. form = urllib.request.OpenerDirector.open(url, params)
is the wrong way to send the request. OpenerDirector.open is an instance method of the class OpenerDirector, not a class method. So that only works if you make an instance and then call the method on that. Like:
Expand|Select|Wrap|Line Numbers
  1. opener = urllib.request.build_opener(....)
  2. form = opener.open(url, params)
That is only useful if you want a special opener, for example with authentication. In the simple cases you can get away with the standard opener by using urlopen:
Expand|Select|Wrap|Line Numbers
  1. form = urllib.request.urlopen(url, params)
(this one tested)
SuperMetroid's Avatar
Newbie
 
Join Date: Oct 2009
Posts: 5
#5: Oct 13 '09

re: Form Value Won't Post/Submit


I'll close this thread, since Piet is helping me elsewhere. Thanks for everything Piet!

No more responses needed here.
Reply

Tags
form, post, python, urllib