Chapter 17. HTTP Post

The Post task is a companion to the standard Ant "Get" task. This task does a post and does not necessarily expect anything in return. Almost always, there will be some sort of returned data, this can be logged or written to a file if needed.

To use this task in your build files, include a task definition like this:


    <taskdef name="post" classname="ise.antelope.tasks.PostTask"/>
   

Basically, an HTTP POST sends name/value pairs to a web server. A very common usage is for html forms for submitting data. A typical use of this task is to send data to a servlet for updating a web page with the status of a build.

This Post task handles cookies and remembers them across calls. This means that you can post to a login form, receive authentication cookies, then subsequent posts will automatically pass the correct cookies. The cookies are stored in memory only, they are not written to disk and will cease to exist upon completion of the build.

The Post task has three ways of specifying the data to be posted. Nested "prop" elements can be used. A "prop" element represents a single name/value pair. The second way is to specify a property file as an attribute to the Post. All properties from the file will be sent as part of the Post data. The third way is to just type in some defined Ant properties. Is it allowed to use all three ways at once, that is, read some properties from a file, specify others via "prop" elements, and just type in some Ant properties.

Table 17.1. Post Task Attributes

AttributeDescriptionDefaultRequired
toThe URL of the remote server to send the post.NoneYes
encodingCharacter encoding for the name/value pairs.UTF-8No
logfileThe name of a file to write any response to. Ignored if wantresponse is set to false.NoneNo
appendShould an existing log file be appended to or overwritten?True, append to an existing file.No
fileA file to read POST data from. All Ant properties contained in this file will be resolved (that is, ${} syntax will be expanded to their values) prior to sending the file contents.NoneNo
maxwaitThe maximum amount of time in seconds to wait for the data to be sent or for a response from the remote server. Setting this to zero means wait forever.180 (3 minutes)No
wantresponseWhether to wait for a response from the remote server or not. In many cases this can greatly improve the performance of the build script as the server response may be large and useless to the script. Use this with caution - while the response from the server may not be required for the client, the server may require that the client accept the response prior to processing the post data.trueNo
propertyIf set and wantresponse, put the response from the remote server into this property.NoneNo
failonerrorWhether the build should fail if the post fails.falseNo

Post supports nested "prop" elements. As an HTTP POST basically sends a list of names and values, the "prop" element represents one name/value pair. A Post may contain any number of "prop" elements.

Table 17.2. Prop Attributes

AttributeDescriptionDefaultRequired
nameThe name of a property to post.NoneYes
valueThe value associated with the name.NoneNo

The "value" attribute is not strictly required. This provides a short-cut method in cases where the property data is an already-defined Ant property. Suppose the build file has this property defined:


    <property name="src.dir" value="/home/user/project/src"/>

Then the following are equivalent:


    <prop name="src.dir"/>
    <prop name="src.dir" value="${src.dir}"/>
    <prop name="src.dir" value="/home/user/project/src"/>

Defined Ant properties can be entered directly into the post element. Again, suppose the build file has this property defined:


    <property name="src.dir" value="/home/user/project/src"/>

Then the following are equivalent:


    ${src.dir}
    <prop name="src.dir"/>
    <prop name="src.dir" value="${src.dir}"/>
    <prop name="src.dir" value="/home/user/project/src"/>

I googled for the URL in the following example.


    <property name="test.val" value="here's my test value"/>
    <property name="test.val2" value="second test value"/>
    <post to="http://wwwj.cs.unc.edu:8888/tang/servlet/tangGetPostServlet"
        verbose="true">
        <prop name="prop1" value="val1 ${test.val}"/>
        <prop name="prop2" value="val1 value 2"/>
        <prop name="prop3" value="val got some spaces %funky ^$* chars"/>
        <prop name="prop4" value="&amp; do an ampersand like this &amp;amp; or
        Ant will whine"/>
        <prop name="thanks" value="dude, thanks for the echo server!"/>
        <prop name="test.val"/>
        ${test.val2}
    </post>

Also see the Grep task for additional examples.