3 Replies Latest reply on Dec 19, 2009 3:53 PM by nimmagadda_k

    Need help in sending XML via httpservice

    nimmagadda_k

      Hi,

       

         I have a requirement where I need to send an xml input to RESTful webservice developed in java.Please find the code given below that I am using to do this.Not able to do PUT or POST operation which updates a record in the backend.Please let me know whats wrong in my code given below

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
          width="530" height="610">
        <mx:script>
        <![CDATA[
          import import mx.rpc.http.HTTPService;
        
          public function sendXML():void
          {
            var myXML:XML = <ns:account xmlns:ns="http://....."/>   //some valid namespace
                           <ns:name>test</ns:name>
                           <ns:phone>123456789</ns:phone>
                           </ns:account>;

           var httpService:HTTPService = new HTTPService();     httpService = new HTTPService();
           httpService.url = "http://localhost/restfulservice";
           httpService.useProxy = false;
           httpService.method = "PUT";   //also tried POST             
           httpService.send(myXML);
         

          }                 
        ]]>

       


        </mx:script>
        <!-- calling sendXML by clicking on the button below -->
        <mx:Button click="sendXML()"/>    
      </mx:Application>
        • 1. Re: Need help in sending XML via httpservice
          LaVengeance

          Hi,

           

          I'm not sure if this was intentional, but in the code you've given, the XML needs to be assigned as a string. It lacks the wrapping quotes. Again, I'm not sure if you've done this just to illustrate a point.

          And I suggest you use the proxy, If you're not using the proxy, HTTPService uses a flash.net.URLLoader to make the request directly so you're limited to what the player supports.

          Also you have 2 'import' keywords in : import import mx.rpc.http.HTTPService;

           

          And you need not instantiate the httpservice object twice. Below is the code that I think would work :

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
              width="530" height="610">
            <mx:Script>
            <![CDATA[
              import mx.rpc.http.HTTPService;
           
              public function sendXML():void
              {
                var myXML:XML = "<ns:account xmlns:ns=\"http://.....\"> <ns:name>test</ns:name> " +
                        "<ns:phone>123456789</ns:phone></ns:account>";

           

               var httpService:HTTPService = new HTTPService();    
               httpService.url = "http://localhost/restfulservice";
               httpService.useProxy = true;
               httpService.method = "PUT";   //also tried POST            
               httpService.send(myXML);

           

              }                
            ]]>
            </mx:Script>
            <!-- calling sendXML by clicking on the button below -->
            <mx:Button click="sendXML()"/>   
          </mx:Application>

          • 2. Re: Need help in sending XML via httpservice
            saisri2k2 Level 4

            In the last line you can do

             

            var obj:Object = new Object();

            obj["data"]= myXML.toXMLString();

             

            httpService.send( obj );

             

            On the server side get the data from the object and convert the string to XML

            • 3. Re: Need help in sending XML via httpservice
              nimmagadda_k Level 1

              Thanks for your replies.As you suggested I used blazeDS proxy and able to send the XML to the restful webservice which I have done this way

               

              service =

              new HTTPService();

              service.destination =

              "carrierAccountsService";

              service.useProxy =

              true;

              service.method =

              "PUT";

              service.contentType =

              "application/xml";

              service.addEventListener(

              "result", newAccountResult);

              service.addEventListener(

              "fault", httpFault);

               

              var xml:XML = getXMLForNewAccount();

              Alert.show(xml);

              service.send(xml);

               

              and configured the destintation "carrierAccountsService" in proxy-config file.But I have one more problem

               

              Tried to do the DELETE operation same way which accepts three parameters(id,name and comment) as given below

               

               

              var

               

               

              params:Object = {};

              params[

              "id"]= del_id.text;

              params[

              "user"]= "someuser";

              params[

              "comment"]= del_stopComments.text;

              service =

              new HTTPService();

              service.destination =

              "carrierAccountsService";

              service.useProxy =

              true;

              service.method =

              "DELETE";

               

              //Alert.show("params passed are:"+ del_id.text +":"+"currentUser:"+ del_stopComments.text); 

              service.addEventListener(

              "result", deleteAccountResult);

              service.addEventListener(

              "fault", httpFault);

              Alert.show(params.id);   //printing the ID before sending request

              service.send(params);

               

              But when I checked the logs on the server side DELETE request is invoked but the parameters are being passed as NULL.Can some one help me on this?

               

              I am getting bad request error