5 Replies Latest reply on Jan 19, 2012 9:38 PM by pradeep reddy

    Can we invoke RESTful Webservices from FLEX?

    pradeep reddy

      Hi,

       

      am trying to invoke RESTful webservices from flex. am able to work with GET operation but not with POST, PUT and DELETE. If anyone has experienced this.. please suggest a good way to do this.

       

       

      Thanks in advance..!

       

      Thanks

      Pradeep

        • 1. Re: Can we invoke RESTful Webservices from FLEX?
          Claudiu Ursica Level 4

          URLLoader or HTTPService, docs are your firends ...

          C

          • 2. Re: Can we invoke RESTful Webservices from FLEX?
            Claudiu Ursica Level 4

            for the PUT and the rest I think there is a a project on google code rep which is adapting this, here google is your friend

             

            C

            • 3. Re: Can we invoke RESTful Webservices from FLEX?
              oldMster Level 3

              Post works just fine, we've been using it for several years.  RESTful services is the only way we get data into our Flex applications.

              Mark

              • 4. Re: Can we invoke RESTful Webservices from FLEX?
                oldMster Level 3

                Below is our communication class.  This class is used for all of our RESTful calls.  It handles most of the common errors so we don't have to deal with them over and over again in the applications.  It always sets itself up as the listener for the RESULT event, does some initial, common processing, then calls the application RESULT function that is set for this particular instance of the communication class.  It also has code to inspect the url the SWF was loaded from, and build the REST service url based on that, so we can use one class for multiple sites.

                Mark

                 

                /*     Communication Utilities

                */

                 

                 

                package Utility {

                          /*     Importation     */

                          import mx.controls.Alert;

                          import mx.controls.Button;

                          import mx.core.Application;

                          import mx.core.FlexGlobals;

                          import mx.rpc.events.FaultEvent;

                          import mx.rpc.events.ResultEvent;

                          import mx.rpc.http.mxml.HTTPService;

                 

                 

                          public class Communication extends HTTPService {

                                    /*     Non-Bindable Public Properties     */

                                    public var Cursor:Boolean = true;

                                    public var DebugFlag:Boolean = false;

                                    public var Message:String;

                                    public var Params:Object;

                                    public var Receiver:Function = null;

                                    public var ReceiverAdditional:Function = null;

                                    public var ErrorHandler:Function = null;

                                    public var btnCancel:Button = null;

                 

                 

                                    /*     Bindable Public Properties     */

                                    [Bindable] static public var Status:Boolean = true;

                                    [Bindable] static public var StatusMessage:String = "";

                 

                 

                                    /*     Static Public Properties     */

                                    static public var HTTPS:Boolean = true;

                 

                 

                                    /*     Private Properties     */

                                    private var Retry:Number = 0;

                 

                 

                                    /*     Constructor     */

                                    public function Communication() {

                                              resultFormat = "e4x";

                                              method = "POST";

                                              var thisapp:Object = FlexGlobals.topLevelApplication;

                                              var baseurl:String = FlexGlobals.topLevelApplication.url;

                                              var urlarray:Array = baseurl.split("/")

                                              var pattern1:RegExp = new RegExp("http://[^/]*/");

                                              var pattern2:RegExp = new RegExp("https://[^/]*/");

                                              //the swf can be loaded from either http or https, but the data requests are always

                                              //sent with https, NO EXCEPTIONS!!!!

                                              if (pattern1.test(baseurl) || pattern2.test(baseurl)) { 

                                                        var location:String = new String();

                                                        var requestfrom:String = urlarray[3].toString();

                                                        requestfrom = requestfrom.toUpperCase();

                                                        switch(requestfrom) {

                                                                  case "ONE":

                                                                            location = "SERVERONE";

                                                                            if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {

                                                                                      FlexGlobals.topLevelApplication.isProduction = true;

                                                                            }

                                                                            break;

                                                                  case "TWO":

                                                                            location = "SERVERTWO";

                                                                            if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {

                                                                                      FlexGlobals.topLevelApplication.isProduction = false;

                                                                                      DebugFlag = true;

                 

                                                                            }

                                                                            break;

                                                                  case "THREE":

                                                                            location = "SERVERTHREE";

                                                                            if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {

                                                                                      FlexGlobals.topLevelApplication.isProduction = true;

                                                                            }

                 

                 

                                                                            break;

                                                                  case "FOUR":

                                                                            location = "SERVERFOUR";

                                                                            if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {

                                                                                      FlexGlobals.topLevelApplication.isProduction = true;

                                                                            }

                 

                 

                                                                            break;

                                                                  case "FIVE":

                                                                            location = "SERVERFIVE";

                                                                            if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {

                                                                                      FlexGlobals.topLevelApplication.isProduction = false;

                                                                                      DebugFlag = true;

                 

                                                                            }

                                                                            break;

                                                                  case "SIX":

                                                                            location = "SERVERSIX";

                                                                            if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {

                                                                                      FlexGlobals.topLevelApplication.isProduction = true;

                                                                            }

                 

                 

                                                                            break;

                                                                  case "SIXTEST":

                                                                            location = "SERVERSIX";

                                                                            if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {

                                                                                      FlexGlobals.topLevelApplication.isProduction = false;

                                                                                      DebugFlag = true;

                 

                                                                            }

                                                                            break;

                                                                  case "SEVEN":

                                                                            location = "SERVERSEVEN";

                                                                            if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {

                                                                                      FlexGlobals.topLevelApplication.isProduction = true;

                                                                            }

                                                                            break;

                                                                  case "SEVENTEST":

                                                                            location = "SERVERSEVEN";

                                                                            if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {

                                                                                      FlexGlobals.topLevelApplication.isProduction = false;

                                                                                      DebugFlag = true;

                 

                                                                            }

                                                                            break;

                                                                  case "EIGHTTEST":

                                                                            location = "SERVEREIGHT";

                                                                            if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {

                                                                                      FlexGlobals.topLevelApplication.isProduction = false;

                                                                                      DebugFlag = true;

                                                                            }

                                                                            break;

                                                                  case "EIGHT":

                                                                            location = "SERVEREIGHT";

                                                                            if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {

                                                                                      FlexGlobals.topLevelApplication.isProduction = true;

                                                                            }

                                                                            break;

                                                                  default:

                                                                            location = "SERVERONE";

                                                                            if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {

                                                                                      FlexGlobals.topLevelApplication.isProduction = true;

                 

                                                                            }

                                                                            break;

                                                        }

                                                        url = "https://" + urlarray[2].toString() + "/scripts/mgwms32.dll?MGWLPN=" + location + "&MGWAPP=ENTRY";

                                              }

                                              else {

                                                        //falls to here if run from flash builder since the URL is a local file path

                 

                 

                                                        DebugFlag = true;

                                                        var debugloc:String = "DEBUGSERVER";

                                                        if (FlexGlobals.topLevelApplication.hasOwnProperty("isProduction")) {

                                                                  FlexGlobals.topLevelApplication.isProduction = true;

                                                        }

                 

                                                        url="https://debugserver/scripts/mgwms32.dll?MGWLPN="+debugloc+"&MGWAPP=ENTRY";

                                              }

                                    }

                 

                 

                                    /*     Public Functions     */

                                    // Failed communication with the server

                                    public function Failure(event:FaultEvent):void {

                                              if (btnCancel != null) {

                                                        btnCancel.enabled=false;

                                              }

                                              Status = false;

                                              removeEventListener(FaultEvent.FAULT, Failure);

                                              var fault:String = event.fault.faultString;

                                              var errorpos:Number = fault.indexOf(":",0);

                                              var error:String = fault.substring(0, errorpos);

                                              var invalid:Boolean = false;

                                              if (error == "Error #1085") invalid = true;

                                              if (error == "Error #1090") invalid = true;

                                              if (invalid) {

                                                        StatusMessage = "Received Invalid XML: " + fault.substring((errorpos + 1), fault.length);

                                              } else {

                                                        Retry = Retry + 1;

                                                        if (Retry > 3) {

                                                                  if (HTTPS) {

                                                                            HTTPS = false;

                                                                            Transmit(true);

                                                                            return;

                                                                  }

                                                                  StatusMessage = "Connection Lost";

                                                        } else {

                                                                  Transmit(true);

                                                        }

                                              }

                                    }

                                    // Receive data from the server

                                    public function Receive(event:ResultEvent):void {

                                              if (btnCancel != null) {

                                                        btnCancel.enabled=false;

                                              }

                                              Status = true;

                                              StatusMessage = "";

                                              var errorcode:String = event.result.ErrorCode;

                                              var errormessage:String = event.result.ErrorMessage;

                                              if (errorcode == "") {

                                                        if (Receiver != null) Receiver.call(this, event);

                                                        if (ReceiverAdditional != null) ReceiverAdditional.call(this);

                                                        return;

                                              }

                                              else {

                                                        if (ErrorHandler != null) {

                                                                  ErrorHandler.call(this, event);

                                                                  return;

                                                        }

                                              }

                                              Communication.StatusMessage = errorcode + ": " + errormessage;

                                              Alert.show("Error processing request\n Error Code: " + errorcode + " \n " + errormessage);

                                    }

                                    // Transmit data to the server

                                    public function Transmit(usemessage:Boolean):void {

                                              if (btnCancel != null) {

                                                        btnCancel.enabled=true;

                                              }

                 

                 

                                              Status = true;

                                              if (usemessage) StatusMessage = Message;

                                              addEventListener(ResultEvent.RESULT, Receive);

                                              addEventListener(FaultEvent.FAULT, Failure);

                                              showBusyCursor = Cursor;

                                              if (DebugFlag) {

                                                        Params.DEBUGTRACE = 1

                                              }

                                              if (Params.SCITOKEN == null && Params.SCIAPP != "LOGIN" ) {

                                                        Params.SCITOKEN=FlexGlobals.topLevelApplication.appUser.Token;

                                              }

                                              send(Params);

                                    }

                          }

                }

                • 5. Re: Can we invoke RESTful Webservices from FLEX?
                  pradeep reddy Level 1

                  The Truth about Flex HTTPService

                   

                  So I have unraveled the Truth. (I think)

                  It's more tortured than one would imagine

                  1/ All HTTP GET requests are stripped of headers. It's not in the Flex stack so it's probably the underlying Flash player runtime

                  2/ All HTTP GET requests that have content type other than "application/x-www-form-urlencoded" are turned into POST requests

                  3/ All HTTP POST requests that have no actual posted data are turned into GET requests. See 1/ and 2/

                   

                  4/ All HTTP PUT and HTTP DELETE requests are turned into POST requests. This appears to be a browser limitation that the Flash player is stuck with.


                  What this boils down to in practical terms is that if you want to pass headers in all requests, you should always use POST and you should find another way to communicate the semantics of the operation you "really wanted". The Rails community have settled on passing ?_method=PUT/DELETE as a work around for the browser problems underlying 4/

                  Since Flash adds the wonderful header stripping pain on GET, I'm also using ?_method=GET as a workaround for that. However, since this trips up on 3/,
                  I am passing a dummy object as the encoded POST data. Which means my service needs to ignore dummy posted data on a ?_method=GET request.

                  Crucial at this point to know about 2/. That wasted a bunch of my time.

                  I've built all of this handling into a new RESTService class with MXML markup support so it's possible to pretend this doesn't exist on the client side.

                  Hope this helps someone.