7 Replies Latest reply on Feb 22, 2013 5:39 AM by Mark Nongkhlaw

    Using AsyncHttp and RESTful WCF

    Chris Misztur

      I wanted to see how easy it would be to hit a .NET WCF web service from a Rho app.  It turns out it is pretty easy.

       

      1.  Expose the service operation as a REST method.

      1.JPG

      The ValidateUser operation takes a complex data type that is composed of two strings => username and password.  It feels more natural to pass around JSON to me than XML so the request and response will be JSON.

       

      2.  In addition to our existing 'basicHttpBinding' we have to add a new 'webHttpBinding' to our web.config.

      2.JPG

      3.  The service is hosted in IIS.

      3.JPG

      4.  Then from the client Ruby app we call it like so.

       

              require 'json'

                ...

              rest = "http://some.domain.com/SecurityService.svc/rest/ValidateUser"

              body = {"Username"=>"Chris", "Password"=>"s3cr37"}.to_json

              result = Rho::AsyncHttp.post(

                :url => rest,

                :headers => {"Content-Type"=>"application/json; charset=utf-8", "Content-Length"=>body.length, "my-special-header-1"=>"ABC", "my-other-special-header-2"=>"123"},

                :body => body,

                :callback => url_for(:action => :login_callback))

                ...

                [somewhere in the callback, the body of the result is the returned hash]

                @params["body"]