Call now to talk to an automation expert — (512) 345-2211

Script Library

Sample scripts are provided as-is with no warranty of fitness for a particular purpose. These scripts are solely intended to demonstrate techniques for accomplishing common tasks. Additional script logic and error-handling may need to be added to achieve the desired results in your specific environment.


http_post_file.s

Download

Note: Make sure you really need to do a POST. If you are simply uploading a file it may be simpler to use the SENDFILE command (which uses the HTTP verb PUT instead of POST).

Note: This process has been simplified as of v3.7. If you are using v3.7 or later, see Sample Script #26.

As of v3.6.0, Robo-FTP supports automating communication via HTTP and HTTPS.

One of the more complicated scenarios to automate is the process of communicating interactively with a web site and uploading data via a web form.

This script provides some functions that greatly simplify this task.

HTTP web forms come in 2 basic varieties:

application/x-www-form-urlencoded

This is the default type of form. It can be submitted using either an HTTP GET or an HTTP POST.

Submit via GET

This type is less common and is not the focus of this sample. Automating this type of form is very easy. You simply need to construct a url of the form:

 http://www.myurl.com/subdir/script.asp?field1=value1&field2=value2

This can be accomplished with a simple three-line Robo-FTP script:

 FTPLOGON "www.myurl.com" /servertype=HTTP
 FTPCD "subdir"
 RCVFILE "script.asp?field1=value1&field2=value2" /as "localfilename.html"

This type of form can only be used for submitting ASCII characters.

Submit via POST

This is the most common type of form submission and is also used to submit ASCII characters in field=value pairs as above. The only difference is instead of appending the form data to the url, the data is added to the body of the POST.

The sample script below includes functions for encoding the data properly and submitting the form.

multipart/form-data

This type must be explicitly specified in the <form> tag's enctype attribute like this:

 <form action="http://www.myurl.com" method="post" enctype="multipart/form-data">

This type of form submits data via POST as described above, but can also include a file upload. This is the only type of form that can cover the entire ISO10646 character set.

Uploading a file via this type of form is defined in RFC 2388.

This sample script includes functions that simplify the process of encoding a file upload into the body of the form and submitting it.

The fields, values, and URL used in the script below are examples and will need to be replaced with whatever fields, values, and URL the system you are trying to submit to requires.


  1  BEGINFUNCTIONS
  2    FUNCTION InitPostData file boundary
  3      SET %myfile = file
  4      SET %myboundary = "--" & boundary
  5      SET %contenttype = "multipart/form-data; boundary=" & boundary
  6      WRITEFILE file %myboundary    
  7    ENDFUNCTION
  8    FUNCTION AddPostField name value 
  9      SET %header = 'content-disposition: form-data; name="' & name & '"'
 10      WRITEFILE %myfile %header /append

 11      WRITEFILE %myfile "" /append

 12      WRITEFILE %myfile value /append

 13      WRITEFILE %myfile %myboundary /append

 14      RETURN
 15    ENDFUNCTION
 16    FUNCTION AddPostFile fieldName file mimeType
 17      SETSUBSTR depth = file "\"
 18      SETNUM depth = depth + 1
 19      SETEXTRACT base = file "\" depth
 20      SET %header = 'content-disposition: form-data; name="' & fieldName & '"; filename="' & base & '"'
 21      SET %mimeHeader = 'Content-Type: ' & mimeType
 22      SET %headFile = %myfile & ".head"
 23      SET %tailFile = %myfile & ".tail"
 24      WRITEFILE %myfile %header /append

 25      WRITEFILE %myfile %mimeHeader /append

 26      WRITEFILE %myfile "Content-Transfer-Encoding: binary" /append

 27      WRITEFILE %myfile "" /append

 28      RENAME %myfile %headFile
 29      WRITEFILE %tailFile %myboundary
 30      SET %command = "copy /b " & %headFile & " + " & file & " + " & %tailfile & " " & %myfile
 31      DOSCMD %command
 32      DELETE %headFile
 33      DELETE %tailFile
 34    ENDFUNCTION
 35  ENDFUNCTIONS
 36  
 37  ;; You should not need to edit anything above this line.
 38  ;; Just set the variables below as appropriate and call
 39  ;; the above functions as demonstrated below.
 40  
 41  SET %tmpdata = "C:\post.bin"
 42  SET %field1 = "field1 value"
 43  SET %field2 = "field2 value"
 44  SET %filename = "C:\files\files.zip" 
 45  ; mimetype below should be based on extension (zip)
 46  SET %mimetype = "application/zip"
 47  ; see http://www.mimetype.org/ or use application/octet-stream
 48  
 49  FTPLOGON "HTTP Site"
 50  ; prepare post data for upload
 51  InitPostData %tmpdata "lakfiqalwuFieldSeperator48pyq489p"
 52  AddPostField "field1" %field1
 53  AddPostField "field2" %field2
 54  AddPostFile "userfile" %filename %mimetype
 55  HTTPPOST "/poster.asp" %tmpdata %postresult /outtype=string /contenttype=%contenttype
 56  DELETE %tmpdata
 57  FTPLOGOFF

Browse complete list of scripts

Need help figuring out where to start?

Our sales staff is available to answer product questions and help you determine if Robo-FTP is a good match for your project.

Call Now: (512) 345-2211