ffledgling
ffledgling

Reputation: 12130

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

Upvotes: 2

Views: 1037

Answers (2)

alecxe
alecxe

Reputation: 473753

What about requests library?

It makes things more simple, see docs on how to make a post request.

I guess you should do smth like this:

import requests

host = "my_host/"
url = host + "show_bug.cgi"

params = {'ctype': 'xml', 
          'excludefield': 'attachmentdata',
          'id': [788604,...,827493],
          'xml': 'XML'}
response = requests.post(url, data=params)

Upvotes: 3

jfs
jfs

Reputation: 414069

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.

Upvotes: 3

Related Questions