Web service
SOA (Service Oriented Architecture) Principles
A service-oriented architecture (SOA) is an architectural pattern in computer software design in...
Restful Web Services is a lightweight, maintainable, and scalable service that is built on the REST architecture. Restful Web Service, expose API from your application in a secure, uniform, stateless manner to the calling client. The calling client can perform predefined operations using the Restful service. The underlying protocol for REST is HTTP. REST stands for REpresentational State Transfer.
In this REST API tutorial, you will learn-
REST Web services have really come a long way since its inception. In 2002, the Web consortium had released the definition of WSDL and SOAP web services. This formed the standard of how web services are implemented.
In 2004, the web consortium also released the definition of an additional standard called RESTful. Over the past couple of years, this standard has become quite popular. And is being used by many of the popular websites around the world which include Facebook and Twitter.
REST is a way to access resources which lie in a particular environment. For example, you could have a server that could be hosting important documents or pictures or videos. All of these are an example of resources. If a client, say a web browser needs any of these resources, it has to send a request to the server to access these resources. Now REST services defines a way on how these resources can be accessed.
The key elements of a RESTful implementation are as follows:
Resources – The first key element is the resource itself. Let assume that a web application on a server has records of several employees. Let's assume the URL of the web application is http://demo.gtupapers.com. Now in order to access an employee record resource via REST services, one can issue the command http://demo.gtupapers.com/employee/1 - This command tells the web server to please provide the details of the employee whose employee number is 1.
Request Verbs - These describe what you want to do with the resource. A browser issues a GET verb to instruct the endpoint it wants to get data. However, there are many other verbs available including things like POST, PUT, and DELETE. So in the case of the example http://demo.gtupapers.com/employee/1 , the web browser is actually issuing a GET Verb because it wants to get the details of the employee record.
Request Headers – These are additional instructions sent with the request. These might define the type of response required or the authorization details.
Request Body - Data is sent with the request. Data is normally sent in the request when a POST request is made to the REST web services. In a POST call, the client actually tells the REST web services that it wants to add a resource to the server. Hence, the request body would have the details of the resource which is required to be added to the server.
Response Body – This is the main body of the response. So in our RESTful API example, if we were to query the web server via the request http://demo.gtupapers.com/employee/1 , the web server might return an XML document with all the details of the employee in the Response Body.
Response Status codes – These codes are the general codes which are returned along with the response from the web server. An example is the code 200 which is normally returned if there is no error when returning a response to the client.
The below diagram shows mostly all the verbs (POST, GET, PUT, and DELETE) and an REST API example of what they would mean.
Let's assume that we have a RESTful web service is defined at the location. http://demo.gtupapers.com/employee . When the client makes any request to this web service, it can specify any of the normal HTTP verbs of GET, POST, DELETE and PUT. Below is what would happen If the respective verbs were sent by the client.
Let's take a look from a perspective of just a single record. Let's say there was an employee record with the employee number of 1.
The following actions would have their respective meanings.
Restful mostly came into popularity due to the following reasons:
But in the end, no matter what the environment is, the end result should always be the same that they should be able to talk to each other. Restful web services offer this flexibility to applications built on various programming languages and platforms to talk to each other.
The below picture gives an example of a web application which has a requirement to talk to other applications such Facebook, Twitter, and Google.
Now if a client application had to work with sites such as Facebook, Twitter, etc. they would probably have to know what is the language Facebook, Google and Twitter are built on, and also on what platform they are built on.
Based on this, we can write the interfacing code for our web application, but this could prove to be a nightmare.
Facebook, Twitter, and Google expose their functionality in the form of Restful web services. This allows any client application to call these web services via REST.
Can you imagine the amount of effort to try and code applications on these devices to talk with normal web applications? Again Restful API's can make this job simpler because as mentioned in point no 1, you really don't need to know what is the underlying layer for the device.
An application or architecture considered RESTful or REST-style has the following characteristics
For example, if you delete a resource from a server using the DELETE command, you cannot expect that delete information to be passed to the next request.
In order to ensure that the resource is deleted, you would need to issue the GET request. The GET request would be used to first get all the resources on the server. After which one would need to see if the resource was actually deleted.
The REST architecture is based on a few characteristics which are elaborated below. Any RESTful web service has to comply with the below characteristics in order for it to be called RESTful. These characteristics are also known as design principles which need to be followed when working with RESTful based services.
This is the most fundamental requirement of a REST based architecture. It means that the server will have a RESTful web service which would provide the required functionality to the client. The client send's a request to the web service on the server. The server would either reject the request or comply and provide an adequate response to the client.
The concept of stateless means that it's up to the client to ensure that all the required information is provided to the server. This is required so that server can process the response appropriately. The server should not maintain any sort of information between requests from the client. It's a very simple independent question-answer sequence. The client asks a question, the server answers it appropriately. The client will ask another question. The server will not remember the previous question-answer scenario and will need to answer the new question independently.
The Cache concept is to help with the problem of stateless which was described in the last point. Since each server client request is independent in nature, sometimes the client might ask the server for the same request again. This is even though it had already asked for it in the past. This request will go to the server, and the server will give a response. This increases the traffic across the network. The cache is a concept implemented on the client to store requests which have already been sent to the server. So if the same request is given by the client, instead of going to the server, it would go to the cache and get the required information. This saves the amount of to and fro network traffic from the client to the server.
The concept of a layered system is that any additional layer such as a middleware layer can be inserted between the client and the actual server hosting the RESTFul web service (The middleware layer is where all the business logic is created. This can be an extra service created with which the client could interact with before it makes a call to the web service.). But the introduction of this layer needs to be transparent so that it does not disturb the interaction between the client and the server.
This is the underlying technique of how RESTful web services should work. RESTful basically works on the HTTP web layer and uses the below key verbs to work with resources on the server
Now in this REST API tutorial, we will learn how to create a Restful web service in ASP.NET:
Web services can be created in a variety of languages. Many integrated development environments can be used to create REST-based services.
In this RESTful API example, we are going to create our REST application in .Net using Visual Studio. In our example, for Restful web services we are going to emulate the following REST service example.
We are going to have a Restful web service which will work on the below set of data.
The below set of data represents an REST API example of having a company which exposes the Tutorial's they have based on the Tutorialid.
| Tutorialid | TutorialName |
| 0 | Arrays |
| 1 | Queues |
| 2 | Stacks |
In our REST API tutorial example, we are going to implement the below Restful Verbs.
Let's follow the below steps in this RESTful API tutorial to create our first RESTful web services, which carries out the above implementation.
Step 1) The first step is to create an empty Asp.Net Web application. From Visual Studio 2013, click on the menu option File->New project.
Once you click on the New Project option, Visual Studio will then give you another dialog box for choosing the type of project and to give the necessary details of the project. This is explained in the next step of this RESTful API tutorial
Step 2) In this step,
Once done you will see the project file created in your solution explorer in Visual Studio 2013.
Step 3) The next step is to create the web service file which is going to have the RESTful web service
In the dialog box which appears, you need to perform the following
Step 4) The next step is to actually make a configuration change to enable this project to complete work with RESTful web services. This requires to make a change to the file called Web.config. This file appears in the same window as the Webservice project file. The file Web.config contains all configurations that make the web application work as it should. The change being made actually allows the application to send and receive data as a pure RESTful web service.
Step 5) The next step in this RESTful API tutorial is to add our code for implementation. All of the below-mentioned code has to be written in the TutorialService.svc file
namespace Webservice.REST
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed
public class TutorialService
{
private static List lst = new List
(new String[] {"Arrays","Queues","Stacks"});
Step 6) Next we will define the code for our GET method. This code will also reside in the same TutorialService.svc file. This code will run whenever we call the service from our browser.
The below method will be used to fulfill the below-mentioned scenario
[WebGet(UriTemplate="/Tutorial")]
public String GetAllTutorial()
{
int count = 1st.Count;
String TutorialList = "";
for (int i = 0; i < count; i++)
TutorialList = TutorialList + lst[i] + ",";
return TutorialList;
}
Code Explanation:-
Step 7) The code below ensures that if a GET call is made to the Tutorial Service with a Tutorial id, then it would return the corresponding Tutorial Name based on the Tutorial id.
[WebGet (UriTemplate = "/Tutorial/{Tutorialid}")]
public String GetTutorialbyID(String Tutorialid)
{
int pid;
Int32.TryParse(Tutorialid, out pid);
return lst[pid];
}
Code Explanation:-
Step 8) The next step is to write up the code for our POST method. This method will be invoked whenever we want to add a string value to our list of Tutorials via the POST method. For example, if you wanted to add the Tutorial name of "Software Testing" then you would need to use the POST method.
Code Explanation:-
Step 9) Finally we are going to add our method to handle the DELETE operation. This method will be invoked whenever we want to delete an existing string value from our list of Tutorials via the DELETE method.
[WebInvoke(Method = "DELETE", RequestFormat = WebMessageFormat.Ison,
UriTemplate = "/Tutorial/{Tutorialid}", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
public void DeleteTutorial(String Tutorialid)
{
int pid;
Int32.TryParse(Tutorialid, out pid);
1st.RemoveAt(pid);
}
Code Explanation:-
Now that we have created our entire web service in the above section. Let's see how we can run the Tutorial service so that it can be invoked from any client.
To run the web service, please follow the below steps
Step 1) Right click on the Project file – Webservice.REST
Step 2) Choose the menu option 'Set as StartUp Project'. This will ensure that this project is run when Visual Studio runs the entire solution
Step 3) The next step is to run the project itself. Now depending on the default browser installed on the system, the appropriate browser name will come next to the run button in Visual Studio. In our case, we have Google Chrome showing up. Just click on this button.
Output:-
When the project is run, you can browse to your TutorialService.svc/Tutorial section, and you will get the below output.
In the above output,
In the above section, we have already seen how to use the browser to execute the 'GET' verb and invoke the 'GetAllTutorial.'
GET Tutorial/Tutorialid - When a client invokes this Restful API, they will be given the Tutorial name based on the Tutorialid sent by the client
In your browser, append the string /1 after the Tutorial word in the URL. If you hit the enter button, you will get the below output
Now you will see the output of Queues which actually corresponds to the number 1 in our list of Tutorial Strings. This means that the 'GetTutorialbyID' method is now being invoked from our Webservice. It also shows that the value of 1 is being passed successfully via the browser to our web service and to our method and that is why we are getting the correct corresponding value of "Queues" in the browser.
POST Tutorial/Tutorialname - When a client invokes this Restful API, the client will submit a request to insert a Tutorialname. The web service will then add the submitted Tutorial name to the collection.
Run the Filddler tool and perform the below steps;
application.
Finally, just click the Execute button in fiddler. This will send a request to the web service to POST the data "Trees" to our web service.
Now, when we browse to the Tutorial URL to show all the strings in our Tutorial list, you will now see the value of "Trees" is also present. This shows that the POST request to the web service was successfully executed and that it was successfully added to our Tutorial List.
DELETE Tutorial/Tutorialid- When a client invokes this Restful API, the client will submit a request to delete a Tutorialname based on the Tutorialid. The web service will then delete the submitted Tutorial name from the collection.
Run the Filddler tool and perform the below steps
application.
Finally, just click the Execute button in fiddler. This will send a request to the web service to DELETE the data "Queues" to our web service.
Now, when we browse to the Tutorial URL to show all the strings in our Tutorial list, you will notice that the value of "Queues" is no longer present.
This shows that the DELETE request to the web service was successfully executed. The element at index no 1 in our list of Tutorial strings was successfully deleted.
Summary
A service-oriented architecture (SOA) is an architectural pattern in computer software design in...
What is an API? API is the acronym for Application Programming Interface. It is a software...
What is WSDL? Web Services Description Language (WSDL) is an XML-based file that basically tells...
What is SOAP? SOAP is an XML-based protocol for accessing web services over HTTP. It has some...
{loadposition top-ads-automation-testing-tools} What is Service Virtualization? Service...
{loadposition top-ads-automation-testing-tools} An API or Application Programming Interface is a...