Raw POST Data from OpenLaszlo Datasets

There are three practical ways to get data from a browser client to a web service:

  • Query string parameters in a GET or a POST request.
  • POST parameters.
  • Raw POST data.

The first two are fairly straightforward: A query string is the part of the URL that comes after the question mark. Post parameters (as name/value pairs) can be added to a POST request by calling setQueryParam() on the dataset. If the client needs to send a large block of data to the server (e.g. an XML document) that data can be assigned to a single POST parameter, you can also send it as pdf with sodapdf.com/merge-pdf/.

The third one is a little more complicated. Essentially, it means all the POST body. i.e. Where the POST parameters would normally live. Older versions of the Flash Player had problems with this, so it was not possible to submit raw POST data with older versions of OpenLaszlo in SOLO mode.

This isn’t an issue today. The Flash Player can submit raw post data. So if you absolutely can’t have even a single POST parameter, you can specify content to be included in the POST body by calling:

dataset.setAttribute("querytype", "POST");
dataset.setQueryParam("lzpostbody", "CONTENT");

Note that the lzpostbody query parameter is a special name that tells OpenLaszlo to use the provided string as the entire POST body. Here’s an example of this in action:

<canvas debug="true" proxied="false">
    <dataset name="ds" src="myService.jsp" type="http" request="false" 
             querytype="POST"/>

    <button>Do Request
        <handler name="onclick"><![CDATA[
            var body = '<rawpostbodycontents>Hello, this is some XML.</rawpostbodycontents>';
            ds.setQueryParam("lzpostbody", body);
            ds.doRequest();
        ]]>
        </handler>
    </button>

    <handler name="ondata" reference="ds">
        Debug.write("RESPONSE:", ds.serialize());
    </handler>
</canvas>

You can download the entire source, complete with a JSP that reads and echoes the raw POST data.

Why I should RTFM

A few days ago, I wrote about how to access the named colors in OpenLaszlo 4.2.0. (Where Have All the Colors Gone?) I even wrote a little example showcasing the colors in the lz.colors array. Then Tucker helpfully directed me to a very complete example in the documentation that showed not only the location of the colors, but also a better way of setting colors.

The W3C provides a list of standard CSS colors, and OpenLaszlo now allows you to use those color names as strings when setting colors.

So in addition to being able to say:

myView.setAttribute("bgcolor", lz.colors.cornflowerblue);

… and of course:

myView.setAttribute("bgcolor", 0x6495ed);

… what’s new is that you can also say:

myView.setAttribute("bgcolor", "cornflowerblue");

Notice that the color name is a string, not a variable. Finally, you can also use the pound (#) notation when setting colors in script:

myView.setAttribute("bgcolor", "#6495ed");

Note that when using pound notation, the color value must be a string.

Where Have All the Colors Gone?

In OpenLaszlo you’ve always been able to specify colors in a variety of ways. In tags, you could use named colors, JavaScript hexadecimal notation and pound (hex) notation:

<view bgcolor="red" width="20" height="20" />
<view bgcolor="0xff0000" width="20" height="20" />
<view bgcolor="#ff0000" width="20" height="20" />

In JavaScript, you could use either the hex notation or the named color:

myview.setAttribute("bgcolor", 0xff0000);
myview.setAttribute("bgcolor", red);

Note that in JavaScript, the color is a global variable; not a string. That’s important, because if you try to use the variable’s name in OpenLaszlo 4.2, you’ll get a Debugger warning:

ERROR @colors.lzx#35: reference to undefined variable ‘red’

That’s because all the colors are now neatly arranged in an array called lz.colors. So in 4.2 you would write:

myview.setAttribute("bgcolor", lz.colors.red);

Another benefit of the colors all being grouped in an array – as opposed to being global – is that you can loop through them. I wrote the following application to display the colors and color names available in OpenLaszlo 4.2.

Click the button to render the color boxes. Move your mouse over the boxes to display the name of the color.

The code is below:

<canvas width="460" height="460" proxied="false">

    <class name="colorbox" width="30" height="30" clickable="true">
        <attribute name="colorname" type="string" />
    </class>

    <class name="colornameoutput" extends="text"
           font="Helvetica, Arial, sans-serif"
           fontsize="24" fontstyle="bold">
        <attribute name="updateDel" 
                   value="$once{new lz.Delegate(this, 'displayColor')}" />
        <method name="displayColor" args="c">
            this.setAttribute("text", "lz.colors." + c.colorname);
        </method>
    </class>

    <simplelayout axis="y" spacing="10" />
     
    <view name="colorboxes" width="100%">
        <wrappinglayout spacing="0" />
    </view>

    <colornameoutput name="outputText">
    </colornameoutput>

    <button options="ignorelayout" width="80%" 
            height="80%" align="center" valign="middle"
            font="Helvetica, Arial, sans-serif"
            fontsize="13" fontstyle="bold">
            Show Me The Colors!
            (Be patient, it takes a moment).
        <handler name="onclick">
            this.setAttribute("visible", false);
            for (var i in lz.colors) {
                var colorValue = lz.colors[i];
                var attrs = {bgcolor: colorValue, colorname: i}; 
                var v = new lz.colorbox(colorboxes, attrs);
                outputText.updateDel.register(v, "onmouseover");
            }
        </handler>
    </button>

</canvas>