#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    1
    Rep Power
    0

    GET vs POST variables...


    Hi,

    Im working on a new project with python as cgi. Ive looking around trying to find out a way to distinguish GET variables from form submitted POST variables.

    For example:

    <form method=post action=process.py?a=1>
    <input type=text name=a>
    <input type=Submit name=Process>
    </form>

    In this case, I need to distinguish between the POST parameter which holds the name and the GET parameter which holds a 1.

    If I use:

    form = cgi.FieldStorage()

    then form["a"] will return a list and Ill have no way of knowing which one is which... is that right?

    Although the sample is a little bit silly, and I could use different variable names, the reason I need this are a little bit more obscure.

    Greetings,

    Aliosha
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    You probably can't use the cgi module then and must do your own parsing of the environment variables.

    On the other hand, mixing GET and POST variables is not such a good idea because it is nonstandard and some browsers apparently handle it badly. See
    http://www.perlmonks.org/index.pl?node_id=284816
    http://www.perlmonks.org/index.pl?node_id=48840

    Those two links are for perl, but it is the same principle really.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    The example you give does not make sense, since you can only pass parameters by either POST or GET, but not both at once. I think if you ran your example then the browser would either ignore the "?a=1" part of the URL, or give an error.

    However you can tell how the parameters were passed by checking the 'REQUEST_METHOD' environment variable, which should be either 'GET' or 'POST'. (I think it can also be 'HEAD', but this is rarely used).

    e.g.

    Code:
    import cgi
    import os
    
    if os.environ['REQUEST_METHOD'] == 'GET':
       print 'processing GET request'
    elif os.environ['REQUEST_METHOD'] == 'POST':
       print 'processing POST request'
    else:
       print 'unknown request type'
    Dave - The Developers' Coach
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I solved this problem while i was writing my net module (which seems to come up alot lattly but) your welcome to the code if it helps you solve your problem or even just as an example!

    http://forums.devshed.com/t129666/s.html&highlight=NET

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo