Search code examples
pythonhttpposturllib2urllib

Issues posting a form using python


I'm using a combination of urllib, urllib2 to POST some form data.

Here is the form I need to submit

<form method="post" action="show_bug.cgi">
            <input type="hidden" name="ctype" value="xml">
            <input type="hidden" name="id" value="788604">
            <input type="hidden" name="id" value="793892">
            <input type="hidden" name="id" value="823569">
            <input type="hidden" name="id" value="823585">
            <input type="hidden" name="id" value="825904">
            <input type="hidden" name="id" value="827493">
            <input type="hidden" name="excludefield" value="attachmentdata">
            <input type="submit" value="XML" id="xml">
</form>

I'm creating a list of tuples of the form [ (name,value), ... ]

Since the submit type element has no name, I have no idea how and if to send that element using urllib/urllib2.

When I try posting this data to show_bug.cgi, I get an HTTP404 Error.

I'm confused about how to POST the form, especially the submit input type. So far I've always assumed that the value and id of the Submit type never mattered, and so far it seems to have worked for me.


This is the post request as captured by TamperData

POST parameters


Solution

  • The following request works:

    from urllib import urlencode
    from urllib2 import urlopen
    
    
    url = 'https://bugzilla.mozilla.org/show_bug.cgi'
    data = urlencode([('ctype', 'xml'), ('id', [788604, 793892]),
                      ('excludefield', 'attachmentdata'),
                      ], doseq=True)
    response = urlopen(url, data)
    print(response.code) # -> 200
    xml = response.read()
    

    if doseq=True then ('id', [788604, 793892]) is encoded as id=788604&id=793892.

    Without doseq, ('id', 788604), ('id', 793892) produce the same result.