[Tutor] Re: HTTPLib and POST problem with '?'

Skip Montanaro skip@pobox.com
Sat, 12 Jan 2002 09:13:34 -0600


    Ladislav> If I use example from Python doc like this
    Ladislav> ####################
    Ladislav> import httplib, urllib
    Ladislav>  params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
    Ladislav>  h = httplib.HTTP("www.musi-cal.com:80")
    Ladislav>  h.putrequest("POST", "/cgi-bin/script.cgi")
    ...
    Ladislav> ################
    Ladislav> it works well but only if the script is like
    Ladislav> http://www.musi-cal.com/cgi-bin/script.cgi
    Ladislav> but if the script is like
    Ladislav> http://www.musi-cal.com/cgi-bin/script.cgi?name=Paul
    Ladislav> (note a part after ? ) it means that PUTREQUEST should be
    Ladislav>  h.putrequest("POST", "/cgi-bin/script.cgi?name=Paul")

    Ladislav> but httplib ignores everything after '?' and sends only
    Ladislav> /cgi-bin/script.cgi
    Ladislav>   again

You can change things a couple ways:

    * If you want to use the POST method, you must always send the
      parameters using a call to the send() method of the HTTP object.  In
      this case, you had

        params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
        ...
        h.send(params)

      If you want the parameter to be "name" with a value of "Paul", change
      the call that sets params to

        params = urllib.urlencode({'name': 'Paul'})

    * Use the GET method instead of the POST method:

        h = httplib.HTTP("www.musi-cal.com:80")
        h.putrequest("GET", "/cgi-bin/script.cgi?name=Paul")
        h.putheader('Accept', 'text/plain')
        h.putheader('Host', 'www.musi-cal.com')
        h.endheaders()
        reply, msg, hdrs = h.getreply()

In any case, make sure when you test your code you are communicating with a
web server that you know and that actually has the URL you are interested
in.  As the webmaster of www.musi-cal.com I can tell you with a fair degree
of certainty there is no CGI script here named "script.cgi".

-- 
Skip Montanaro (skip@pobox.com - http://www.mojam.com/)