using urllib or httplib to post with ENCTYPE=mulitpart/form-data

Kevin Carlson khcarlso at bellsouth.net
Wed Jun 4 23:07:05 EDT 2003


John J. Lee wrote:

>Erm, I'm hesitant to suggest this module, because the version that
>does INPUT TYPE=FILE file upload -- which I'm guessing is what you
>want to do -- 
>
Thanks for the response, John.  Sorry, I should have been more 
specific.  I don't need to upload a file in this situation.  I need to 
post to a form that is hosted by a business partner and they require the 
multipart/form-data type.

I actually only need to send a couple of text inputs to the form, but no 
matter what, the host system acts like I input nothing into the form.  I 
have tried this:

  formData = "--AaB03x\r\n"
  formData += 'Content-Disposition: form-data; name="field1"'
  formData += "\r\n\r\n"
  formData += "somedata\r\n"
  formData += "--AaB03x\r\n"
  formData += 'Content-Disposition: form-data; name="field2"'
  formData += "\r\n\r\n"
  formData += "somemoredata\r\n"
  formData += "--AaB03x--\r\n"
  efd = urllib.quote(formData)

  httpObj = httplib.HTTPS(hostName)
  httpObj.putrequest("POST", postFormName)
  httpObj.putheader('Content-type', 'multipart/form-data; 
boundary=--AaB03x')
  httpObj.putheader('Accept', '*/*')
  httpObj.putheader('Content-length', str(len(formData)))
  httpObj.endheaders()
  httpObj.send(efd)
  err, msg, hdrs = httpObj.getreply()
  data = httpObj.getfile().read()

When I examine the data object, the HTML that I receive includes an 
error message
that I need to include inputs for field1 and field2.

Any additional ideas?






More information about the Python-list mailing list