Showing posts with label Fiddler. Show all posts
Showing posts with label Fiddler. Show all posts

Tuesday, November 1, 2011

Step 8 : WCF RESTful GET JSON List with FIDDLER


http://pantestmb.blogspot.com/2011/10/step-3-from-soap-to-wcf-restful-get.html

In Step 3 there was a sample of transforming a SOAP Web Service into a WCF RESTful one
with "application/xml" Content-Type ;

In step 7 - there are all the details about the full source code

To be accessed from a Android Client the sample must switch from XML to JSON data-content ;

Let's consider that the webservice is installed on the IP : 192.168.61.3 :
http://192.168.61.3/RestServicePost/RestServiceImpl.svc/json/getallpersons

The code behind the above link : 

        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/getallpersons")]
        PersonData[] getJsonPersons();

        public PersonData[] getJsonPersons()
        {
            return getAllPersons();
        }


        public PersonData[] getAllPersons()
        {
            SqlConnection dbConn = new SqlConnection(connStr);
            dbConn.Open();
            string sqlSelect = "select * from users ";
            SqlDataAdapter da = new SqlDataAdapter(sqlSelect, dbConn);
            DataTable dt = new DataTable();
            SqlCommand dbCommand = new SqlCommand(sqlSelect, dbConn);
            da.Fill(dt);
            dbConn.Close();
            List list = new List();
            foreach (DataRow row in dt.Rows)
            {
                // Person target = Activator.CreateInstance();
                PersonData target = new PersonData();
                target.Name = row["Name"].ToString();
                target.User = row["UserName"].ToString();
                target.Email = row["EMail"].ToString();
                target.Password = row["Password"].ToString();
                // DataColumnAttribute.Bind(row,target);
                list.Add(target);
            }
            return list.ToArray();
        }

Pictures about what the code does ;
FIDDLER gets from the webservice all the persons from the database in JSON format :







Wednesday, October 26, 2011

Step 6 : WCF REST & FIDDLER JSON - request body


WCF REST * FIDDLER JSON - request body , headers and content type : 



  [DataContract(Namespace = "")]
    public class PersonData
    {
        [DataMember]
        public string Name { get; set; }


        [DataMember]
        public string User { get; set; }


        [DataMember]
        public string Email { get; set; }


        [DataMember]
        public string Password { get; set; }
    }




    [OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "jsontestpersondatapost")]
        PersonData JsonTestPersonDataPost(PersonData pd);





       public PersonData TestPersonDataPost(PersonData pd)
        {
            var response = new PersonData
            {
                Name = pd.Name,
                User = pd.User,
                Email = pd.Email,
                Password = pd.Password
            };
            
            return response;
            // return pd;
        }


        public PersonData JsonTestPersonDataPost(PersonData pd)
        { 
            return TestPersonDataPost(pd);
        }









POST http://192.168.61.3/RestServicePost/RestServiceImpl.svc/jsontestpersondatapost HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Host: 192.168.61.3
Content-Length: 86


{ "pd" : { "Email":"bo@se.us" , "Name":"amab" , "Password":"egti" , "User":"brac" } } 










HTTP/1.1 200 OK
Content-Length: 99
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET
Date: Wed, 26 Oct 2011 18:31:54 GMT


{"JsonTestPersonDataPostResult":{"Email":"bo@se.us","Name":"amab","Password":"egti","User":"brac"}}










Tuesday, October 25, 2011

POST JSON from Android using HttpClient




POST JSON from Android using HttpClient




Step 5 : Pass multiple body parameters in wcf rest



How pass multiple body parameters in wcf rest

using webinvoke method(Post or PUT)








Even If you have blank namespace  [DataContract(Namespace = "")] you have to put 
xmlns="" in the request body ... ( see the pictures below )
Whithout xmlns="" in the request body , problems can appear 






  [OperationContract]
        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Xml,
            RequestFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "testpersondatapost")]
        PersonData TestPersonDataPost(PersonData pd);


  [DataContract(Namespace = "")]
    public class PersonData
    {
        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string User { get; set; }

        [DataMember]
        public string Email { get; set; }

        [DataMember]
        public string Password { get; set; }
    }


       public PersonData TestPersonDataPost(PersonData pd)
        {
            var response = new PersonData
            {
                Name = pd.Name,
                User = pd.User,
                Email = pd.Email,
                Password = pd.Password
            };
            
            return response;
            // return pd;
        }
























Tuesday, October 18, 2011

Create a POST to a REST WCF Service with Fiddler



How to POST to REST WCF web services with Fiddler

CREATE RESTful WCF Service API Using POST: Step By Step Guide

After making a set of REST web services with WCF I tested them by making requests with a browser. 
This is fine for GET requests however you will need some HTTP building tool such as Fiddler to test a POST, PUT or DELETE REST web service.

To begin with I have a WCF REST web service that intends for a RequestData object to be POSTed:

        [WebInvoke(Method = "POST",
            ResponseFormat = WebMessageFormat.Xml,
            RequestFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "auth")]
       ResponseData Auth(RequestData rData);


The definition of RequestData is very simple:



  public class RequestData
    {
        [DataMember]
        public string details { get; set; }
    }


The definition of ResponseData is quite simple:



public class ResponseData
    {
        [DataMember]
        public string Name { get; set; }


        [DataMember]
        public string Age { get; set; }


        [DataMember]
        public string Exp { get; set; }


        [DataMember]
        public string Technology { get; set; }
    }


The function that gets RequestData and returns ResponseData : 


    public ResponseData Auth(RequestData rData)
        {
            var data = rData.details.Split('|');            
           var response = new ResponseData
                               {
                                   Name = data[0],
                                   Age = data[1],
                                   Exp = data[2],
                                   Technology = data[3]
                               };

            return response;
        }


Note this namespace is very important as it will need to be specified in the XML sent in the request. 
Personally I set my DataContract with no namespace to save having to write it out in the XML every time.

Now open up Fiddler and lets get testing:

Create a POST to your REST service in Fiddler
Go to the Request Builder tab in Fiddler.
Set the verb to POST
Set the URL to the one of your service
Type "Content-Type: application/xml" into the Request Headers. 
If you did give your DataContract a namespace other than blank as I did you will need to specify it in the XML of your Request Body. 
Add your XML to the Request Body like : 





The response : 




Comsuming WCF Services With Android

Create a Simple WCF service against Pubs Database using EF, LINQ



PersonPassport4.asmx - Simple webservice : INSERT UPDATE DELETE



RESTful ASP.NET WCF with SQLite without LINQ: 

  [ServiceContract]

    public interface IService1
    {

    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "Notes/{ID}")]
        [OperationContract]
        Note GetNote(string ID);

   [DataContract]
    public class Note
    {
         [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string Category { get; set; }
        [DataMember]
        public string Subject { get; set; }
        [DataMember]
        public string NoteText { get; set; }
    }

      public Note GetNote(string ID)
        {
            string Sql = "SELECT * FROM NOTE WHERE ID=@ID";
            SQLiteCommand cmd = new SQLiteCommand(Sql, conn);
             cmd.Parameters.AddWithValue("@ID", int.Parse(ID));
            SQLiteDataAdapter da = new SQLiteDataAdapter(Sql, conn);
            da.SelectCommand = cmd;
            DataTable dt = new DataTable();
             da.Fill(dt);
            Note note=null;
            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                note = new Note();
                note.ID = Convert.ToInt32(row["ID"]);
                note.Category = (string)row["Category"];
                note.Subject = (string)row["Subject"];
                note.NoteText = (string)row["NoteText"];
            }
    
            WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
             return note;
        }

Thursday, September 29, 2011

Calling a RESTful Service Using Fiddler


Creating RESTful Services Using WCF


https://docs.google.com/document/pub?id=1T4oQneluGK-6y_-v5_hO5sxw-RenSXgtqUfYve11bdA
http://channel9.msdn.com/blogs/pdc2008/tl35 - WCF: Developing RESTful Services


Create a Console App, add references for : 
System.ServiceModel, 
System.Runtime.Serialization, 
System.ServiceModel.Web
                                                             and hit F5 in Visual Studio. 




System.ServiceModel.Web - if there is reference problem : 

In "project properties" make sure your "target framework" is set to : .NET Framework 4
and not: .NET Framework 4 Client Profile, or any lower .NET version.






FIDDLER , Request Builder , Execute : 
POST         http://127.0.0.1:8000/customers         HTTP/1.1





























a) GET & POST  : 

b) C# console : 

c) SQL Server +  Android client : 


APPLICATION POOL : 



http://dotnetninja.wordpress.com/2008/05/02/rest-service-with-wcf-and-json/
http://www.dotnetspark.com/kb/1373-step-by-step-tutorial-rest-enabled-service.aspx


Web.Config : 





REST Based WCF Service in ASP.NET 3.5