FormCollection

Metreos.Types.Http.FormCollection

Summary

The FormCollection allows developers easy access to the name-value pairs contained in the body of an HTTP POST request.

Usage

FormCollection provides a number of utilities to help developers quickly access the names and corresponding values in the post content from a form. The body of the request must be structured in conformance with the form content type of application/x-www-form-urlencoded. In general, a form in a web page or most web libraries will follow this convention. More detailed information can be found in the HTML 4.01 specification.

To initialize a FormCollection variable with the post content from an incoming GotRequest event, set the InitializeWith property of a variable in the event handler to the event parameter Body.

To read a form field in from the FormCollection type, use the .NET indexer syntax on the variable, specifying the name of the form field as the argument to the indexer. For example, if one had a variable of the name post, and wanted to know the value of a form field called 'user', then the C# snippet post["user"] would resolve to that value. Note that one can use the GetValues( System.String ) method to return a string array, which is the more appropriate option if your form defines the same field name more than once.

To programmatically determine all the form fields passed in, three members defined by FormCollection in particular are of interest. One is the Count property, which will indicate how many fields are defined in total. Using that as a loop count, one can then use the GetNameAt( System.Int32 ) method and this[ System.Int32 ] indexer to determine the name and value(s) of each form field defined in the request. For example, one can build a CustomCode action that takes a variable named post as an argument, and iterates over every form field like the below example:

Example 36.1. Dynamically Determining Contents of the FormCollection


public static string Execute(Metreos.Types.Http.FormCollection post)
{
    for(int i = 0; i < post.Count; i++)
    {
        string post = post.GetNameAt(i);

        string post = post[i];  // note that one may want to not use the indexer and instead 
                                // use post.GetValuesAt(i) if the field has multiple string values

        //perform logic...
    }

    return String.Empty;
}


Remarks

None.

Parseable Inputs
.NET TypeDescription
StringAccepts a string that follows the form content type of application/x-www-form-urlencoded. In common practice this value comes from the Body event parameter of the GotRequest event.
Accessible Public Methods
Method NameDescription
GetNameAtRetrieves the name of the form field at the given index.
GetValuesSpecify the name of a field form of interest. The resulting string array is a list of all values for the named field. If no occurrences of the form field exist, then a null is returned.
GetValuesSpecify the index of a field form of interest. The resulting string array is a list of all values for the field at the specified index. If no occurrences of the form field exist, then a null is returned. If an index is specified that falls ouf of the valid positions in the FormCollection variable, then an unhandled exception will occur and the script instance will halt.
ResetClears the variable to an uninitialized state.
GetEnumeratorThis method should not be used. Instead use Count in conjunction with a for loop structure.
Accessible Public Properties
Property Name.NET TypeDescription
CountSystem.Int32Returns the number of form fields defined. Note that multiple form fields with the same name all count as only one field.
Accessible Public Indexer Properties
.NET Indexer Type.NET Return TypeDescription
System.StringSystem.StringSpecify the name of a field form of interest. The resulting string array is a list of all values for the named field. If no occurrences of the form field exist, then a null is returned.
System.Int32System.StringSpecify the index of a field form of interest. The resulting string is the value of the form field found at the specified index. If no occurrences of the form field exist, then a null is returned. If an index is specified that falls ouf of the valid positions in the FormCollection variable, then an unhandled exception will occur and the script instance will halt.