5.1 Using HTTPService

This section provides a simple introduction to using the Flex HTTPService component to interface with the REST API.

5.1.1 Creating a Session using HTTPService

The following code sample illustrates creating a Web 2.0 Connect session using the REST API and the native HTTPService component available in Flex. The principles in this example are very similar to the JavaScript examples in Section 4.0, Getting Connected.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application
  xmlns:mx="http://www.adobe.com/2006/mxml"
  horizontalAlign="left"
  verticalGap="15"
  horizontalGap="15"
  layout="absolute"
  height="258" width="523">
  <mx:Script>
  <![CDATA[
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    private function createSession() : void
    {
      w2cService.cancel();
      var params : Object = new Object();
      params.username = "admin";
      params.password = "formula";
      w2cService.send(params);
    }
    private function sessionCreateHandler(event : ResultEvent) : void
    {
      responseText.text = event.result.toString();
    }
    private function sessionCreateFault(event : FaultEvent) : void
    {
      responseText.text = event.fault.toString();
    }
   ]]>
  </mx:Script>
  <mx:HTTPService 
    id="w2cService"
    url="http://manor-as08-v:8080/moweb2cn/rest/session"
    resultFormat="xml"
    result="sessionCreateHandler(event)"
    fault="sessionCreateFault(event)"
  />
  <mx:Button
    label="Create Session"        
    click="createSession()"
    textAlign="center"
    x="451" y="218"
  />
  <mx:TextArea
     id="responseText"
     text="Click Logon to create a session"
     wordWrap="true"
     x="10" y="10"
     width="503" height="200"
  />
</mx:Application>

This source code for this sample can be cut and pasted into a new Flex MXML file and compiled into a SWF file.

When Create Session is clicked, a new Web 2.0 Connect session is created. If create session is successful, the field displays the XML response; or, if the create session fails, the error message from the server is displayed.

Subsequent requests to create a session before a session times out results in error messages from the server alerting users that they are already logged in.

5.1.2 Closing a Session using HTTPService

The following code expands on the create session example and adds a function to terminate an existing session. The closeSession() function uses a declarative HTTPService instance to overcome the lack of support for native HTTP DELETE method in Flex. The Flex HTTPService component only supports HTTP methods PUT and DELETE in proxy mode.

<?xml version="
1.0" encoding="utf-8"?>
<mx:Application  xmlns:mx="http://www.adobe.com/2006/mxml"
  verticalGap="16"
  horizontalGap="16"
  layout="absolute"
  height="256" width="830">
  <mx:Script>
  <![CDATA[
    import mx.rpc.events.FaultEvent;
    import mx.collections.ArrayCollection;
    import mx.rpc.events.ResultEvent;
    private function createSession() : void
    {
      var params : Object = new Object();
      params.username = "admin";
      params.password = "formula";
      w2cService.cancel();
      w2cService.send(params);
    }
    private function createSessionHandler(event : ResultEvent) : void
    {
      responseText.text = "Session created\n" + event.result.toString();
    }
    private function closeSession() : void
    {
      var w2cHttpService : HTTPService = new HTTPService();
      w2cHttpService.addEventListener(ResultEvent.RESULT, closeSessionHandler);
      w2cHttpService.headers = {"X-HTTP-Method-Override" : "DELETE"};
      w2cHttpService.method = "POST";

      w2cHttpService.contentType = "application/xml";
      w2cHttpService.resultFormat = "xml";
      w2cHttpService.url = "http://manor-as08-v:8080/moweb2cn/rest/session";
      w2cHttpService.send(new XML("<response/>"));
    }    private function closeSessionHandler(event : ResultEvent) : void
    {

      responseText.text = "Session closed\n" + event.result.toString();    }
     private function httpServiceFaultHandler(event : FaultEvent) : void
    {
      responseText.text = "Ooops!\n" + event.fault.toString();
    }  ]]>
  </mx:Script>
  <mx:HTTPService
    id="w2cService"
    url="http://manor-as08-v:8080/moweb2cn/rest/session"
    resultFormat="xml"
    result="createSessionHandler(event)"
    fault="httpServiceFaultHandler(event)"
  />
  <mx:TextArea
    id="responseText"
    text="Click button to create a session"
    fontSize="20" fontFamily="Courier New"
    x="10" y="10"
    width="811" height="200"
    wordWrap="true"
  />
  <mx:Button
    label="Create Session"        
    click="createSession()"
    textAlign="center"
    x="593" y="224"
  />
  <mx:Button
    label="Close Session"
    click="closeSession()"
    textAlign="center"
    x="711" y="224"
  />
</mx:Application>

The workaround uses the X‑HTTP-Method-Override modifier in the request header to perform a DELETE request. This method should also work with firewalls that can be configured to block HTTP operations using the PUT and DELETE methods. While the RPC closeSession() method is simpler this workaround is the only way to terminate a session using the REST API.

Remember, with Web 2.0 Connect it is possible to mix and match protocols so there is no reason why an application cannot initiate and manage sessions using RPC while performing simpler read operations using the HTTPService component for ease of its data binding capabilities.