PlanetJava
Custom Search

java-grinder-user
[Top] [All Lists]

Re: multipart form submission... a better tutorial?

Subject: Re: multipart form submission... a better tutorial?
Date: Sun, 14 Dec 2008 13:54:44 +0000
Hi Scott,
You are specifying the file data twice, once in the files variable and
once in the parameters variable. I think you should remove
     NVPair('body:form:documentFile\"; filename=\"'+filename1+'',''+zipfile+''),
from the parameters. (Which then means you don't need to explicitly read
the file into zipfile; Codecs.mpFormDataEncode does that for you).
You probably also want to change
     files = ( NVPair('self', 'document/'+filename1+''), )
to
     files = ( NVPair('body:form:documentFile', 'document/'+filename1+''), )
If you would like this to be part of the documentation, you'll need to
jump through a hoop or two: 1. figure out the "upload twice" problem; 2.
tidy it up so it reads less like a set of notes; 3. format it using
Apache Forrest 's mark up language
(http://forrest.apache.org/dtdx/document-v20.dtdx.html), see
http://grinder.svn.sourceforge.net/viewvc/grinder/trunk/documentation/src/documentation/content/xdocs/g3/tutorial-perks.xml?view=markup
for an example.
- Phil
Scott Russell wrote:
> Folks,
>           I have been through a very difficult time, doing multipart form 
> uploads.  I thought it would be good, to at least share my experiences, with 
> the group.  Maybe this should be a "How to" on the tutorial page.  There is 
> also a small issue that this seems to upload the file twice(from 
> observation through the proxy), but in practice, the file only uploads 
> once(if anyone has a fix, do let me know).
>
>
>
> Ok multipart form submission, from scratch....
>
> 1. start proxy.
>
> 2. do your stuff on the browser and upload something into your website
>
> 3. obtain your capture.py and http-data-0.dat file.
> (note, if you are uploading a small text file, there will be no 
> "http-data-0.dat" file, it will be included as part of the "capture.py" file)
>
> In what follows, I will assume you have an http-data-0.dat file, however the 
> technigue is the same regardless....
>
> Ok, Your http-data-0.dat file will look similar to the following.....
>
> -----------------------------137357244014788080171010743019
> Content-Disposition: form-data; name="body:form:documentType"
>
> mydoc
> -----------------------------137357244014788080171010743019
> Content-Disposition: form-data; name="body:form:shipType"
>
> merchant
> -----------------------------137357244014788080171010743019
> Content-Disposition: form-data; name="body:form:tonnage"
>
> tonnage
> -----------------------------137357244014788080171010743019
> Content-Disposition: form-data; name="body:form:documentFilename"
>
> scott.zip
> -----------------------------137357244014788080171010743019
> Content-Disposition: form-data; name="body:form:documentFile"; 
> filename="scott.zip"
> Content-Type: application/octet-stream
>
> PK
> ***lots and lots of control characters... a binary file basically(scott.zip), 
> removed from this script ***
> ***to make it easier to read ***
>
> -----------------------------137357244014788080171010743019
> Content-Disposition: form-data; name="body:form:captcha"
>
>
> -----------------------------137357244014788080171010743019
> Content-Disposition: form-data; name="body:form:dispositionCode"
>
>
> -----------------------------137357244014788080171010743019
> Content-Disposition: form-data; name="source"
>
> body:form:uploadButton
> -----------------------------137357244014788080171010743019--
>
> Good, now in your capture.py, you will find the page that does the upload.  
> Something like the following:-
>
>
>    def page4(self):
>      """POST submit_document.faces (request 401)."""
>      result = request401.POST('/company/documents/submit/submit_document',
>        request401.__target__.data,
>        ( NVPair('Content-Type', 'multipart/form-data; 
> boundary=---------------------------137357244014788080171010743019'), ))
>      self.token_bodyformdocumentFilename = \
>        httpUtilities.valueFromHiddenInput('body:form:documentFilename') # 
> 'scott.zip'
>      self.token_bodyformdispositionCode = \
>        httpUtilities.valueFromHiddenInput('body:form:dispositionCode') # ''
>      self.token_bodyformuniqueFilename = \
>        httpUtilities.valueFromHiddenInput('body:form:uniqueFilename') # 
> 'scott(scott5_-600084327542045163112...'
>
>      return result
>
>
> Right, now we have the data and the script.  So now we need to modify the 
> script, using the http-data-0.log file as our reference, we then change the 
> page4 code in script capture.py to the following.....
>
>
>    def page4(self, p1statevalue, filenamestr):
>      """POST submit_document (request 401)."""
>      # this has all been almost completely replaced
>      # you need the http-data=0.dat file from the oringal capture
>      # you have got that right?
>      # look through that file and put in the appropriate NV pairs
>      # also make sure you have the correct mime type in the
>      # jdk1.6.0_07/jre/lib/content-types.properties file...
>      # this appears as the line "Content-Type: application/octet-stream"
>      # in the http-data-0.dat file, for zip files
>      # add your mime type to this file if not already there
>      headers = zeros(1, NVPair)
>      filename1=filenamestr
>      print "Filename", filename1
>      # note the "rb", we want to read the file in binary!
>      # for xml files we would use only "r", as it is just
>      # a text file
>      file1 = open("document/"+filename1+"", 'rb')
>      print " have just opened Filename", filename1
>      zipfile=file1.read()
>      file1.close()
>      print "have just read Filename", filename1
>      # keep all the document files we are going to upload
>      # in their own folder called document
>      files = ( NVPair('self', 'document/'+filename1+''), )
>      parameters = ( NVPair('body:form:documentType', 'mydoc'),
>          NVPair('body:form:shipType', 'merchant'),
>          NVPair('body:form:tonnage', '4000-10000'),
>          NVPair('body:form:documentFilename', ''+filename1+''),
>          NVPair('body:form:documentFile\"; 
> filename=\"'+filename1+'',''+zipfile+''),
>          NVPair('body:form:captcha', ''),
>          NVPair('body:form:dispositionCode', ''),
>          NVPair('source', 'body:form:uploadButton'),)
>      data = Codecs.mpFormDataEncode(parameters, files, headers)
>      print "about to make request"
>      result = request401.POST('/company/document/submit/submit_document',
>      data, headers)
>      self.token_bodyformdocumentFilename = \
>        httpUtilities.valueFromHiddenInput('body:form:documentFilename') # 
> 'scott.zip'
>      self.token_bodyformdispositionCode = \
>        httpUtilities.valueFromHiddenInput('body:form:dispostionCode') # ''
>      page8filename=self.token_bodyformuniqueFilename = \
>        httpUtilities.valueFromHiddenInput('body:form:uniqueFilename') # 
> 'scott{scott_-600084327542045163112...'
>
>
>      return result
>
>
>
> OK, now we are good to go.... let's run it...... but we will put it through 
> the grinder proxycapture to check
> that we have everything in the right place....
>
> Uncomment the line below in capture.py as follows....
> connectionDefaults.setProxyServer("localhost", 8001)
>
> Now start your grinder proxy.... and then run the capture.py script....
>
> Output should look like the following(file http-data-0.dat from the new 
> capture!):-
>
>
> ----------ieoau._._+2_8_GoodLuck8.3-ds0d0J0S0Kl234324jfLdsjfdAuaoei-----
> Content-Disposition: form-data; name="body:form:documentType"
>
> mydoc
> ----------ieoau._._+2_8_GoodLuck8.3-ds0d0J0S0Kl234324jfLdsjfdAuaoei-----
> Content-Disposition: form-data; name="body:form:shipType"
>
> merchant
> ----------ieoau._._+2_8_GoodLuck8.3-ds0d0J0S0Kl234324jfLdsjfdAuaoei-----
> Content-Disposition: form-data; name="body:form:tonnage"
>
> tonnage
> ----------ieoau._._+2_8_GoodLuck8.3-ds0d0J0S0Kl234324jfLdsjfdAuaoei-----
> Content-Disposition: form-data; name="body:form:documentFilename"
>
> scott.zip
> ----------ieoau._._+2_8_GoodLuck8.3-ds0d0J0S0Kl234324jfLdsjfdAuaoei-----
> Content-Disposition: form-data; name="body:form:documentFile"; 
> filename="scott.zip"
>
> PK
> ***lots and lots of control characters... a binary file basically(scott.zip), 
> removed from this script ***
> ***to make it easier to read ***
>
> ----------ieoau._._+2_8_GoodLuck8.3-ds0d0J0S0Kl234324jfLdsjfdAuaoei-----
> Content-Disposition: form-data; name="body:form:captcha"
>
>
> ----------ieoau._._+2_8_GoodLuck8.3-ds0d0J0S0Kl234324jfLdsjfdAuaoei-----
> Content-Disposition: form-data; name="body:form:dispositionCode"
>
>
> ----------ieoau._._+2_8_GoodLuck8.3-ds0d0J0S0Kl234324jfLdsjfdAuaoei-----
> Content-Disposition: form-data; name="source"
>
> body:form:uploadButton
> ----------ieoau._._+2_8_GoodLuck8.3-ds0d0J0S0Kl234324jfLdsjfdAuaoei-----
> Content-Disposition: form-data; name="body:form:documentFile"; 
> filename="scott.zip"
> Content-Type: application/octet-stream
>
> PK
> ***lots and lots of control characters... a binary file basically(scott.zip), 
> removed from this script ***
> ***to make it easier to read ***
> ----------ieoau._._+2_8_GoodLuck8.3-ds0d0J0S0Kl234324jfLdsjfdAuaoei-----
>
>
> Things to notice here.  The file appears to upload twice.  I have looked into 
> this, and am unable to stop this from happening.  Maybe someone can 
> point out the error of my ways here.  But it works, just not the way I 
> thought it would in practice.
> Notice also the "Content-Type: application/octet-stream" appears after the 
> second occurance of the file being uploaded.
>
>
>
> Ok, you should now be in a postion to upload multipart forms in both text and 
> binary format....
>
> Other resources, that help here are:-
>
> http://grinder.sourceforge.net/g3/manual.html#form
>
> http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLConnection.html#setFileNameMap(java.net.FileNameMap)
>
> http://www.w3.org/TR/html401/interact/forms.html#form-content-type
>
>
>
>                                      regards
>
>                                       Sc0tt....
>
>   
------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
msgmiddle
<Prev in Thread] Current Thread [Next in Thread>
Current Sitemap | © 2012 planetjava | Contact | Privacy Policy