Thursday, January 22, 2009

Flex HTTPService: When POST is GET

A relatively infrequently encountered nuance of Flex 3's MXML/ActionScript's HTTPService that one may run into occurs when the HTTPService.method public property is set to "POST", but the server actually receives a GET HTTP method instead of the anticipated POST. As documented in Http Service Get vs Post Issue? IS this a bug? and HttpService Post Shows as Get on Server Logs, this occurs when the HTTPService with the method set to "POST" has its send method invoked without any parameters.

In the unlikely event that it is important to have the POST request treated as POST on the server despite having no parameters to pass, one can use a dummy parameter to cause this to happen. The remainder of this blog posting will focus on code examples of this.

The HTTPService can be declared as an MXML tag as shown next.

<mx:HTTPService id="httpService"
result="resultHandler(event)" />

The HTTPService instance shown above can have its method set to HTTP POST and be invoked as shown in the next ActionScript snippet.

httpServiceProxied.method = "POST";

Although the method is clearly set to POST, if no parameters are passed, it will actually be treated like a GET instead of a POST. A dummy object can be added to force the POST to be treated like a POST as shown in the next ActionScript code snippet.

const dummyObject:Object = { "key" : "value" };
httpServiceProxied.method = "POST";

Using the dummy object as shown above will force the POST to be treated as a POST. Note that parameters for an HTTPService invocation can be specified not only in the send() method, but can also be specified in the HTTPService declaration with the <mx:request> element.

While it at first seems a little strange that a POST is treated as a GET if no parameter is supplied, it does not seem nearly as strange when one considers that an HTTP POST is designed to serve as a resource-changing method and a parameter (enclosed entity) will typically be associated with such functionality. Conversely, an HTTP GET is a safe and idempotent method designed for retrieval of data and is probably the more usual HTTP method of the two to be called when no parameter is specified.

Finally, it is worth noting here that when the proxied HTTPService is used (such as with BlazeDS), POST is used when the method is set to POST regardless of whether a parameter is included or not.


Tomas Sancio said...

Thanks man! Your answer with the "dummy object" was perfect. You see, I tried to pass an XML file with Flex Builder 3 as a parameter but it would not work as it did with Flex 2.

Therefore, when I created a new dummy object with just one property called "key" and its value equal to the string representation of the XML object, it went all the way to the server without any hiccups.


@DustinMarx said...


Thanks for the feedback. I especially appreciate your taking the time and effort to respond with how this helped you because what you needed to do is a twist on what I discussed in my post. Your description might help someone else.

Thanks again.


Matteo Garganigo said...

Thank you so much!!!