5 Replies Latest reply on Oct 5, 2006 5:12 PM by Matlus

    Question about posting parameters using HTTPService

    jpwrunyan Level 1
      In the docs for HTTPService it states:

      send () method

      public override function send(parameters:Object = null):AsyncToken
      Executes an HTTPService request. The parameters are optional, but if specified should be an Object containing name-value pairs or an XML object depending on the contentType.

      Well, that is a little vague to me and I have actually run into a problem at this point.

      I want to send an array to the server through the HTTPRequest but it looks like it is being sent as a comma separated string even though the data is definately set as an Array inside Flex.

      ex:
      params[anArray] = new Array(1, 2, 3);

      httpRequest.send(params)

      but the server gets: "1,2,3"

      In .jsp it is possible to send String[] arrays through the POST method. Is this NOT possible in Flex? If not, why?
        • 1. Re: Question about posting parameters using HTTPService
          mike_morearty Level 1
          By "an Object containing name-value pairs", I suspect it means this (although I haven't tried it):

          var params:Object = new Object();
          params.firstName = "mike";
          params.lastName = "morearty";
          httpRequest.send(params);

          Or, the other syntax that yields the same result:

          var params:Object = { firstName: "mike", lastName: "morearty" };
          httpRequest.send(params);

          As for whether it can send String[] arrays, I don't know; try something like

          params = { anArray: new Array(1,2,3) }

          But if that doesn't work, can the server side you're using handle XML? If so, then you could pass an XML object instead of an Object, and pass that instead:

          var params:XML =
          <array>
          <item> ... </item>
          <item> ... </item>
          <item> ... </item>
          </array>;
          • 2. Re: Question about posting parameters using HTTPService
            mike_morearty Level 1
            I wrote "can the server side you're using handle XML?" Actually I'm not sure if there are any restrictions there, it might just work. For a sample of sending XML to PHP, see the sample in the user comments near the bottom of http://livedocs.macromedia.com/flex/2/langref/mx/rpc/http/HTTPService.html
            • 3. Question about posting parameters using HTTPService
              jpwrunyan Level 1
              quote:

              Originally posted by: mike_morearty
              As for whether it can send String[] arrays, I don't know; try something like

              params = { anArray: new Array(1,2,3) }


              the above should work the same as
              params[anArray] = new Array(1, 2, 3);

              the reason I use brackets and not explicit property naming in curly braces is that the property name is dynamically taken from a list of xml attribute names. Anyway, I have tested the Flex data, and it is definately giving me the correct result: object.name = [value1, value2] so I can't imagine the written syntax is a problem.

              I wonder if Array is just automatically converted to string... I wonder what happens with:
              var valueList:Array = [1, 2, 3];
              var anArray:Object = new Object();
              for (var i:uint = 0; i<valueList.length; i++) {
               anArray[String(i)] = valueList[ i ];
              }
              params.arrayListValue = anArray

              maybe if I use Object in this way I can send an array that gets read as String[]

              otherwise, I can just have them split the string up on the server side. But it means we have to worry about arrays like:
              ["1,000", "2,000"] becoming strings: "1,000,2,000" and how to parse them. etc...
              just sort of a pain. And then of course the server side guys are complaining that if .jsp can do it, why can't Flex?

              Finally, XML is an option, but the framework we are using was made originally for .jsp so using XML would entail some additional fixing.
              • 4. Re: Question about posting parameters using HTTPService
                Matlus Level 1
                the send() method expects a dynamic object in the case where you're sending an "object". In other words, you can't send a custom object that you've defined properties on.

                On the server side, this object is received as name=value pairs so the server side guys shouldn't have to deal with anything special. In other words, if you use the "POST" method that they'll see the parameters just like they do if you were to use an html <form method="post"> tag, provided you also set the contentType to HTTPService.CONTENT_TYPE_FORM

                An issue with the HTTPService is that exceptions thrown on the server side and not handled correctly on the Flex side. Specifically, you'll get an exception, but you won't get the actual exception message. This (from what I've been told) is a limitation in the way the FLASH player interacts with browsers and having to conform to the "least common denominator" in browsers.
                • 5. Re: Question about posting parameters using HTTPService
                  Matlus Level 1
                  FYI:
                  I've had to develop an HttpSocket class (that aggregates the HTTPService) class the essentially lets you work on the Flex side as if you were in a regular GUI application using an httpSocket.

                  Since we don't use dynamic objects we even have ultility classes that "convert" an instance of a non-dynamic object into a dynamic object "behind the scenes", so the server side doesn't need to change specifially for Flex apps.

                  In order to be able to handle expcetions, we've had to plug in an "interceptor" (in ASP.NET they are called HttpModules) that essentially traps the exception (in the case that the request was made via a Flex app), set the http status code back to 200, and sends the exception message back in a way that the HttpSocket can detect that it's really an exception and not a normal respose and it in turn, throws the exception.