Monday, April 22, 2013

How to test RESTful service

 We created a Restful Service. Now we shall see how to test that restful service.
I am going to use Fiddler to post the request on Restful API. It is free tool which help us to record the traffic over http(s). You can download fiddler from here 

1.       Take the URL (Address to post the data) as shown in the screenshot below

2.       Open the fiddler and Select the option “Composer” from tab. As shown in the screenshot below

3.       From the composer screen you need to select the action type (Verb) address and content type.
Once you select the options as showing in the screenshot below type the string in “Request Body” you want to send to the Restful service.


4.       Once everything is prepared. Press “Execute” button from right top corner of the “Address Bar”. Please refer the screenshot below

5.       After getting the result you can validate the response code and message as shown below. Here in the below screenshot you will see the response code is “400” that means bad request.

So here question is very important. Since, we have string parameter in contract and we are posting string through fiddler so why we are getting error 400 (Bad request).
[WebInvoke(Method ="POST",UriTemplate ="/interface") ]
        string GetData(string value);

Here an interest point comes into the picture. In restful service we can send the XML or streamed data.
So we shall handle this situation with two different ways
a)      Doing some changes in request
b)      Change in contract.

Change in Request:

If, you refer the screenshot of step one. You will notice that the request is as below

<string xmlns=""> String content

So now we need to write our request in this format.

<string xmlns=""> Hello, This is my first Restful service.

Now post this above mentioned request and we shall get the following response:

<string xmlns=""> You entered:
 Hello, This is my first Restful service.

Change in Contract:
                Now change the string parameter type to Stream so the new contract will be like (Change in IPost.cs interface)

   [ServiceContract(Namespace = "http://localhost/Services/MyRestfulService/2013/04/")]
    public interface IPost

        [WebInvoke(Method ="POST",UriTemplate ="/interface") ]
        Stream GetData(Stream value);

                And similarly change in implementation of MyPosts.svc.cs file

[ServiceBehavior(Namespace = "http://localhost/Services/MyRestfulService/2013/04/")]
    public class MyPosts : IPost
        public Stream GetData(Stream value)
            StreamReader reader = new StreamReader(value);
            string inputValue = reader.ReadToEnd();
            var outputValue = string.Format("You entered: {0}", inputValue);

            var outStream =
                    new MemoryStream(ASCIIEncoding.Default.GetBytes(outputValue));

            return outStream;


Now put the plain test in request as shown in below screenshot and press execute button

You will get the result as expected. As shown in screenshot below.

So like this we can test the simple restful service.

Post a Comment