Loading ...
Sorry, an error occurred while loading the content.

1825Question on consuming REST

Expand Messages
  • Venkat M
    Apr 2, 2012
      Hi All,
       
      I have a basic question on consuming a rest web service. It
      would be great if someone can help me out. Thanks in advance.
       
      I have a web service definition as below.
       
      Login Call
      definition:
       
      <resource path="/Login">
      <method id="Login" name="POST">
      <request>
      <representation mediaType="application/json"/>
      </request>
      <response>
      <representation mediaType="application/json"/>
      </response>
      </method>
      </resource>
       
      I make a JQuery
      Ajax call like
       
      $('#softlogin').click(function() {
                //alert('Login Check - With User Values');
                   var uid = $('#username').val();
                   var pwd = $('#password').val();
                $.ajax({
                   url: 'http://localhost:8080/Services/Login',
                   type: 'POST',
                   async: 'async',
                   data: JSON.stringify({'userid':uid,'password':pwd}),
                   contentType: "application/json",
                   dataType: "json",
                   success: function(data){ 
                     if (data.status == 'OK')
      {
                       key = data.key;
                       document.cookie = "session_key"+ "="+ key;
                       //alert('Success -
      finally'); 
                       $('#content').html('User:
      '+ uid + '  &  Session Key :'+data.key);
                     }
                  }
               }).error(function()
      {
                 alert('An error Occoured');
               });
            });
       
      Once the Login call is success, I am setting up the cookie
      session_key with the Key that is returned in the Login call success.
       
      And to Logout.
       
      The web service definition is like
       
      <resource path="/Logout">
      <method id="Logout" name="POST">
      <request>
      <param xmlns:xs="http://www.w3.org/2001/XMLSchema"%c2%a0name="Cookie"%c2%a0style="header"%c2%a0type="xs:string"%c2%a0path="session_key"/>
      </request>
      <response>
      <representation mediaType="*/*"/>
      </response>
      </method>
      </resource>
       
      In JQuery Ajax I write like
       
      function getCookie(c_name)
            {
            var i,x,y,ARRcookies=document.cookie.split(";");
            for (i=0;i<ARRcookies.length;i++)
            {
              x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
              y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
              x=x.replace(/^\s+|\s+$/g,"");
              if (x==c_name)
                {
                return unescape(y);
                }
              }
            }
           
            $('#termmodule').click(function()
      {
                 var session_key = getCookie("session_key");
                 //alert('Cookie fetched is:' + session_key);
                 $.ajax({
                   url: 'http://localhost:8080/Services/Logout%e2%80%99,
                   type: 'POST',
                   async: 'async',
                   data: JSON.stringify({'session_key':session_key}),
                   //headers: {'Cookie' : session_key},
                   contentType: "application/json",
                   dataType: "json",
                   success: function(data){ 
                     if (data.status == 'OK')
      {
                       alert('Session Logged out);
                       $('#content').html(data);
                     }
                  }
               }).error(function()
      {
                 alert('An error Occoured');
               });
            });
       
      This is not working out. It fails. I am not sure why.
      Is this the right way we pass cookie parameter.
       
      I was also given the testing snippet from REST web service
      developer like:
       
      Response res =
      given().cookie("session_key", session_key).post("http://localhost:8080/Services/Logout");
      It is with cookie
       
      Please help me.
       
      Cheers,
      Venkat.

      [Non-text portions of this message have been removed]
    • Show all 2 messages in this topic