banner



How To Send Data Via Restful Web Services

What is Restful Spider web Services?

Restful Spider 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. Residual stands for REpresentational State Transfer.

In this REST API tutorial, you lot will learn-

  • RESTful Cardinal Elements
  • Restful Methods
  • Why Restful
  • Restful Architecture
  • RestFul Principles and Constraints
  • Create your get-go Restful web service in ASP.Internet
  • Running your get-go Restful spider web service
  • Testing your first Restful web service

RESTful Key Elements

Residue Web services accept actually 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 earth which include Facebook and Twitter.

Remainder is a way to admission resources which lie in a particular environment. For instance, y'all could have a server that could exist 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 asking to the server to access these resource. Now Remainder services defines a fashion on how these resources can be accessed.

The fundamental elements of a RESTful implementation are every bit follows:

  1. Resources – The starting time key element is the resource itself. Let presume that a web application on a server has records of several employees. Let's assume the URL of the spider web awarding is http://demo.guru99.com. Now in order to admission an employee tape resource via Residue services, one can consequence the command http://demo.guru99.com/employee/1 – This control tells the web server to delight provide the details of the employee whose employee number is 1.
  2. Request Verbs – These describe what you want to do with the resource. A browser bug a GET verb to instruct the endpoint it wants to get information. Still, there are many other verbs available including things like Postal service, PUT, and DELETE. And then in the case of the example http://demo.guru99.com/employee/1 , the web browser is really issuing a GET Verb because it wants to get the details of the employee record.
  3. Asking Headers – These are boosted instructions sent with the request. These might define the blazon of response required or the authorization details.
  4. Request Torso – Data is sent with the request. Information is ordinarily sent in the request when a Post request is made to the REST web services. In a Mail service call, the client actually tells the REST web services that it wants to add a resources to the server. Hence, the request body would have the details of the resource which is required to be added to the server.
  5. Response Body – This is the master body of the response. So in our RESTful API example, if we were to query the web server via the asking http://demo.guru99.com/employee/1 , the spider web server might return an XML document with all the details of the employee in the Response Body.
  6. Response Status codes – These codes are the full general codes which are returned forth with the response from the spider web server. An case is the code 200 which is normally returned if at that place is no error when returning a response to the client.

Restful Methods

The beneath diagram shows mostly all the verbs (POST, GET, PUT, and DELETE) and an REST API instance of what they would mean.

Let's assume that we accept a RESTful web service is defined at the location. http://demo.guru99.com/employee . When the customer 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 past the client.

  1. POST – This would exist used to create a new employee using the RESTful spider web service
  2. GET – This would be used to go a listing of all employee using the RESTful web service
  3. PUT – This would be used to update all employee using the RESTful web service
  4. DELETE – This would exist used to delete all employee using the RESTful services

Allow's take a await from a perspective of simply a single tape. Let's say there was an employee tape with the employee number of 1.

The following deportment would have their respective meanings.

  1. Mail service – This would non exist applicable since we are fetching information of employee 1 which is already created.
  2. GET – This would be used to get the details of the employee with Employee no as i using the RESTful web service
  3. PUT – This would be used to update the details of the employee with Employee no as 1 using the RESTful web service
  4. DELETE – This is used to delete the details of the employee with Employee no as i

RESTful Web Services

Why Restful

Restful mostly came into popularity due to the following reasons:

1. Heterogeneous languages and environments – This is one of the fundamental reasons which is the same as we have seen for Lather as well.

  • It enables web applications that are built on various programming languages to communicate with each other
  • With the assistance of Restful services, these spider web applications can reside on different environments, some could be on Windows, and others could be on Linux.

But in the end, no matter what the environs is, the end consequence should e'er exist 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 beneath picture gives an instance 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 equally 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 spider web application, only this could prove to exist a nightmare.

Facebook, Twitter, and Google betrayal their functionality in the class of Restful web services. This allows whatever client awarding to call these web services via REST.

RESTful Web Services

two. The consequence of Devices – Nowadays, everything needs to work on Mobile devices, whether information technology exist the mobile device, the notebooks, or fifty-fifty car systems.

Tin you imagine the amount of attempt to effort and code applications on these devices to talk with normal spider web applications? Again Restful API's can make this job simpler because as mentioned in point no one, you actually don't need to know what is the underlying layer for the device.

three. Finally is the event of the Cloud – Everything is moving to the deject. Applications are slowly moving to cloud-based systems such as in Azure or Amazon. Azure and Amazon provide a lot of API'due south based on the Restful architecture. Hence, applications now need to exist developed in such a way that they are made uniform with the Deject. So since all Cloud-based architectures work on the Remainder principle, it makes more sense for web services to be programmed on the Rest services based compages to make the all-time utilise of Cloud-based services.

Restful Architecture

An application or architecture considered RESTful or REST-way has the following characteristics

1. Land and functionality are divided into distributed resources – This means that every resource should be accessible via the normal HTTP commands of GET, Postal service, PUT, or DELETE. And then if someone wanted to become a file from a server, they should be able to issue the GET request and get the file. If they want to put a file on the server, they should be able to either result the Mail or PUT request. And finally, if they wanted to delete a file from the server, they can issue the DELETE request.

ii. The compages is customer/server, stateless, layered, and supports caching

  • Client-server is the typical architecture where the server tin be the web server hosting the application, and the customer can be as unproblematic as the web browser.
  • Stateless means that the state of the awarding is not maintained in Remainder. For example, if you lot delete a resource from a server using the DELETE control, you cannot expect that delete data to exist passed to the adjacent request.

In club to ensure that the resource is deleted, you would need to issue the GET request. The Become request would be used to get-go get all the resources on the server. After which one would demand to see if the resource was actually deleted.

RESTFul Principles and Constraints

The Residuum architecture is based on a few characteristics which are elaborated below. Whatsoever RESTful spider web service has to comply with the below characteristics in club for it to be called RESTful. These characteristics are also known every bit design principles which need to exist followed when working with RESTful based services.

  1. RESTFul Client-Server

    RESTful Web Services

This is the nearly key requirement of a REST based architecture. It means that the server will take a RESTful web service which would provide the required functionality to the client. The customer 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 customer.

  1. Stateless

The concept of stateless means that information technology's up to the client to ensure that all the required information is provided to the server. This is required so that server tin process the response appropriately. The server should not maintain whatsoever 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 accordingly. The client will ask another question. The server volition not call up the previous question-answer scenario and will need to answer the new question independently.

  1. Cache

RESTful Web Services

The Cache concept is to help with the problem of stateless which was described in the last point. Since each server customer asking 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 get to the server, and the server volition give a response. This increases the traffic beyond the network. The enshroud is a concept implemented on the client to store requests which have already been sent to the server. So if the aforementioned asking is given past the client, instead of going to the server, information technology would go to the enshroud and get the required information. This saves the amount of to and fro network traffic from the customer to the server.

  1. Layered Organization

The concept of a layered system is that any boosted layer such equally a middleware layer tin 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 tin be an actress service created with which the client could collaborate 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 customer and the server.

  1. Interface/Uniform Contract

This is the underlying technique of how RESTful web services should piece of work. RESTful basically works on the HTTP web layer and uses the below key verbs to work with resources on the server

  • Postal service – To create a resources on the server
  • Become – To call back a resource from the server
  • PUT – To change the land of a resources or to update information technology
  • DELETE – To remove or delete a resource from the server

Create your first Restful spider web service in ASP.NET

Now in this REST API tutorial, we will learn how to create a Restful spider web service in ASP.NET:

Web services tin be created in a variety of languages. Many integrated evolution environments tin be used to create Residual-based services.

In this RESTful API example, nosotros are going to create our Rest awarding in .Internet using Visual Studio. In our case, for Restful web services we are going to emulate the post-obit REST service example.

Nosotros are going to have a Restful web service which will work on the below set up of information.

The below set of data represents an Residual API example of having a visitor which exposes the Tutorial's they accept based on the Tutorialid.

Tutorialid TutorialName
0 Arrays
i Queues
2 Stacks

In our Rest API tutorial example, we are going to implement the below Restful Verbs.

  1. GET Tutorial – When a client invokes this Restful API, they volition be given the unabridged set of Tutorials available from the web service.
  2. GET Tutorial/Tutorialid – When a client invokes this Restful API, they volition be given the Tutorial name based on the Tutorialid sent by the client.
  3. POST Tutorial/Tutorialname – When a client invokes this Restful API, the client will submit a request to insert a Tutorialname. The spider web service will then add together the submitted Tutorial name to the collection.
  4. 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.

Let'southward follow the below steps in this RESTful API tutorial to create our first RESTful spider web services, which carries out the above implementation.

How to Create Your First Resful Web Service

Footstep one) Create new project.
The first step is to create an empty Asp.Net Spider web application. From Visual Studio 2013, click on the carte du jour option File->New project.

RESTful Web Services

Once you click on the New Project option, Visual Studio will then give yous another dialog box for choosing the blazon of project and to give the necessary details of the project. This is explained in the next pace of this RESTful API tutorial

Step 2) Enter project proper name and location.

  1. Ensure to starting time cull the RESTful web services C# spider web template of ASP.NET Web application. The project has to be of this type in society to create web services project. Past choosing this options, Visual Studio volition then comport out the necessary steps to add together required files which are required by whatever spider web-based application.
  2. Give a name for your project which in our case has been given as "Webservice.REST".
  3. Then ensure to give a location, where the project files will be stored.

RESTful Web Services

Once done you volition encounter the projection file created in your solution explorer in Visual Studio 2013.

RESTful Web Services

Stride 3) Create the web service file.
The next footstep is to create the web service file which is going to have the RESTful web service

  1. Start Right-click on the project file every bit shown below

RESTful Web Services

  1. In this step,
    1. Right-click on the project file
    2. Choose the pick "Add together->new item."

RESTful Web Services

In the dialog box which appears, yous demand to perform the following

  1. Choose the pick of WCF Service (Ajax-enabled) – Cull a file of this type, it causes the Visual studio to add some basic lawmaking which helps one create a RESTful web service. WCF stands for Windows Advice Foundation. WCF is a library for applications of various platforms or the same platform to communicate over the various protocols such equally TCP, HTTP, HTTPS. Ajax basically is Asynchronous JavaScript and XML. AJAX allows spider web pages to exist updated asynchronously past exchanging small amounts of information with the server behind the scenes.
  2. Adjacent requite a name for the service which is TutorialService in our example.
  3. Finally, click the Add together button to add the service to the solution.

RESTful Web Services

Footstep four) Make a configuration.
The next footstep is to actually make a configuration change to enable this project to complete work with RESTful web services. This requires to make a modify to the file called Web.config. This file appears in the same window as the Webservice projection file. The file Web.config contains all configurations that make the spider web application work equally it should. The modify existence made really allows the awarding to transport and receive data as a pure RESTful web service.

  1. Click on the Web.config file to open the code

RESTful Web Services

  1. Notice for the line <enableWebScript>

RESTful Web Services

  1. Change the line to <webHttp>

RESTful Web Services

Pace 5) Add our code for implementation.
The next step in this RESTful API tutorial is to add our code for implementation. All of the beneath-mentioned code has to exist written in the TutorialService.svc file

  1. The offset bit is to add together code to stand for our data which will be used in our plan. So we are going to accept a listing of cord variables with values of "Arrays", "Queues" and "Stacks". This will represent the tutorials name available through our hosting spider web service.

RESTful Web Services

namespace Webservice.REST { 	[ServiceContract(Namespace = "")] 	[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Immune 	public class TutorialService 	{ 		private static List<String> lst = new List<String> 		(new String[] {"Arrays","Queues","Stacks"});          

Step 6) Define the code for our GET method.
Side by side we will ascertain the code for our Become method. This code will also reside in the aforementioned 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

  • If a user wants a list of all Tutorials available, then the below code would need to be written to accomplish this.

RESTful Web Services

[WebGet(UriTemplate="/Tutorial")]  public Cord GetAllTutorial() { 	int count = 1st.Count; 	String TutorialList = ""; 	for (int i = 0; i < count; i++) 	TutorialList = TutorialList + lst[i] + ","; 	render TutorialList; }          

Code Explanation:-

  1. The first line of code is the most of import. It is used to ascertain how we tin telephone call this method via a URL. And so if the link to our web service is http://localhost:52645/TutorialService.svc and if we append the '/Tutorial' to the URL as http://localhost:52645/TutorialService.svc/Tutorial , the higher up lawmaking will be invoked. The attribute of 'WebGet' is a parameter which allows this method to exist a RESTful method so that it can be invoked via the GET verb.
  2. This department of code is used to go through our list of strings in the 'lst' variable and return all of them to the calling plan.

Step 7) Return the output.
The lawmaking below ensures that if a GET telephone call is made to the Tutorial Service with a Tutorial id, then it would return the corresponding Tutorial Name based on the Tutorial id.

RESTful Web Services

[WebGet (UriTemplate = "/Tutorial/{Tutorialid}")]  public String GetTutorialbyID(String Tutorialid) { 	int pid; 	Int32.TryParse(Tutorialid, out pid); 	return lst[pid]; }          

Code Explanation:-

  1. The start line of lawmaking is the most important. It is used to ascertain how we tin phone call this method via a URL. So if the link to our web service is http://localhost:52645/TutorialService.svc and if we suspend the '/Tutorial/{Tutorialid}' to the URL, then nosotros would be able to call the web service as http://localhost:52645/TutorialService.svc/Tutorial/1 as an example. The web service would then need to render the Tutorial name which had the Tutorial id#one.
  2. This section of code is used to return the "Tutorial name" which has the Tutorial id passed to the web method.
  • By default, what needs to be remembered is that any is passed to the URL in the browser is a string.
  • But you accept to remember that the Index to our listing has to be an integer, so we are calculation the necessary code to first catechumen the Tutorialid to an Integer then use it to access the index position in our list and
  • Then render the value to the calling program accordingly.

Footstep 8) Write the code for POST method.
The adjacent step is to write up the code for our Postal service method. This method will exist invoked whenever we desire to add a string value to our list of Tutorials via the Mail method. For instance, if you lot wanted to add the Tutorial name of "Software Testing" and then yous would need to use the Mail service method.

RESTful Web Services

Code Caption:-

  1. The first line is the 'WebInvoke' aspect which has been attached to our method. This allows the method to be invoked via the Mail service telephone call. The RequestFormat and ResponseFormat aspect have to be mentioned as JSON, since when posting values to a RESTFul web service, the values have to exist in this format.
  2. The second line of lawmaking is used to add the string value passed via the Mail service telephone call to our existing list of Tutorial strings.

Step 9) Add method to handle the DELETE performance.
Finally we are going to add our method to handle the DELETE operation. This method will exist invoked whenever we want to delete an existing cord value from our list of Tutorials via the DELETE method.

RESTful Web Services

[WebInvoke(Method = "DELETE", RequestFormat = WebMessageFormat.Json, 	UriTemplate = "/Tutorial/{Tutorialid}", ResponseFormat = WebMessageFormat.Json, 	BodyStyle = WebMessageBodyStyle.Wrapped)] 	 public void DeleteTutorial(Cord Tutorialid) { 	int pid; 	Int32.TryParse(Tutorialid, out pid); 	1st.RemoveAt(pid); }          

Code Explanation:-

  1. The get-go line is the 'WebInvoke' attribute which has been fastened to our method. This allows the method to be invoked via the POST phone call. The RequestFormat and ResponseFormat attribute have to be mentioned as JSON, since when posting values to a RESTFul spider web service, the values take to be in this format. Annotation that the Method parameter is being set to "DELETE." This means that whenever we result the DELETE verb, this method volition be invoked.
  2. The second line of code is used to take the Tutorialid sent via the DELETE call and subsequently delete that id from our list. (The Int32 function in code is used to convert the Tutorial ID from a string variable to an integer).

Running your kickoff Restful spider web service

Now that we accept created our entire spider web service in the above section. Allow's see how we can run the Tutorial service so that it tin can be invoked from any client.

To run the spider web service, delight follow the below steps

Step 1) Right click on the Projection file – Webservice.REST

RESTful Web Services

Step two) Cull the carte du jour option 'Set equally StartUp Project'. This will ensure that this projection is run when Visual Studio runs the entire solution

RESTful Web Services

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 push button in Visual Studio. In our case, we have Google Chrome showing up. But click on this push.

RESTful Web Services

Output:-

When the project is run, you can browse to your TutorialService.svc/Tutorial section, and you will get the below output.

RESTful Web Services

In the above output,

  • Yous can see that the browser is invoking the 'GET' verb and executing the 'GetAllTutorial' method in the web service. This module is used to brandish all the Tutorials exposed by our web service.

Testing your first Restful web service

In the above section, nosotros have already seen how to use the browser to execute the 'Go' verb and invoke the 'GetAllTutorial.'

  1. Permit's now utilize the browser to execute the following use case scenario.

Become Tutorial/Tutorialid – When a client invokes this Restful API, they will be given the Tutorial proper noun based on the Tutorialid sent by the client

In your browser, append the cord /1 afterwards the Tutorial give-and-take in the URL. If you striking the enter push, you will get the below output

RESTful Web Services

Now yous will encounter the output of Queues which actually corresponds to the number ane in our listing of Tutorial Strings. This means that the 'GetTutorialbyID' method is now existence invoked from our Webservice. Information technology as well 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.

  1. Adjacent let'southward swallow our web service by executing the below scenario. For this, you demand to install the tool called "Fiddler" which is a gratis downloadable tool from the site.

Post Tutorial/Tutorialname – When a client invokes this Restful API, the client will submit a request to insert a Tutorialname. The web service will and so add the submitted Tutorial proper name to the drove.

Run the Filddler tool and perform the beneath steps;

  1. Go to the composer department. This is used to create requests which can be submitted to any webapplication.
  2. Make sure the request type is "POST" and the correct URL is existence hit, which in our case should be http://localhost:52645/TutorialService.svc/Tutorial
  3. Make sure the Content-Type is marked equally application/json. Remember that our POST request method in our Web service only accepts json style information so nosotros need to ensure this is specified when we are sending a request to our application.
  4. Finally, we need to enter our data. Remember that our method for POST accepts a parameter chosen 'str.' So here we are specifying that nosotros want to add a value called "Trees" to our collection of Tutorial names and ensure that it is tagged to the str variable name.

Finally, just click the Execute button in fiddler. This volition ship a request to the web service to POST the data "Trees" to our spider web service.

RESTful Web Services

Now, when nosotros browse to the Tutorial URL to show all the strings in our Tutorial list, yous 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 information technology was successfully added to our Tutorial List.

RESTful Web Services

  1. Next let's swallow our web service past executing the below scenario. For this also nosotros need to use the fiddler tool

DELETE Tutorial/Tutorialid- When a customer invokes this Restful API, the client will submit a request to delete a Tutorialname based on the Tutorialid. The spider web service will then delete the submitted Tutorial proper noun from the collection.

Run the Filddler tool and perform the below steps

  1. Go to the composer department. This is used to create requests which tin exist submitted to any webapplication.
  2. Make sure the request type is "DELETE" and the right URL is being striking, which in our example should exist http://localhost:52645/TutorialService.svc/Tutorial. Ensure that the id which is used to delete a string in the list sent via the URL as a parameter. In our REST example, nosotros are sending 1 so this will delete the 2nd chemical element in our drove which is "Queues".

Finally, just click the Execute button in fiddler. This will send a request to the web service to DELETE the data "Queues" to our spider web service.

RESTful Web Services

Now, when nosotros 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 alphabetize no i in our list of Tutorial strings was successfully deleted.

RESTful Web Services

Summary

  • REST stands for REpresentational State Transfer. REST is used to build web services that are lightweight, maintainable, and scalable in nature.
  • More and more applications are moving to the Restful architecture. This is because there are a lot of people now using mobile devices and a wider diverseness of applications moving to the cloud.
  • The main aspects of Remainder are the resource which reside on the server and the verbs of Become, Post, PUT and DELETE, which can be used to work with these resources.
  • Visual Studio and.Internet can exist used to create Restful web services.
  • When Testing web services for POST and PUT, you need to utilize another tool called fiddler which can be used to send the POST and PUT asking to the server.

How To Send Data Via Restful Web Services,

Source: https://www.guru99.com/restful-web-services.html

Posted by: bussfirmervis.blogspot.com

0 Response to "How To Send Data Via Restful Web Services"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel