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
        public string details { get; set; }

The definition of ResponseData is quite simple:

public class ResponseData
        public string Name { get; set; }

        public string Age { get; set; }

        public string Exp { get; set; }

        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: 


    public interface IService1

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

    public class Note
        public int ID { get; set; }
        public string Category { get; set; }
        public string Subject { get; set; }
        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();
            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;

No comments:

Post a Comment