Published May 27, 2017 by with 18 comments

Introduction to Rest Web Services in Xamarin

This is a quick introduction to some of the concepts that come up when you are doing rest-based networking.

You have a lot of options when you're talking to various servers. If the server wants you to use sockets of course you can do that .
If you want to use soap you can do that. 
If you have a rest-based server out there you can also do that .

Now we're just going to talk about rest and sits by far the most common. Here’s some statistics from a few years ago.




And you can see how dominant rest has become .One of the main drivers for the success of rest is that it's easy to access from JavaScript clients . Also there's a lot of JavaScript clients out there on the web and of course servers want to make it easy for all those clients to access them so they typically provide a rest style API.

Rest means representational state transfer and the term comes from a PhD dissertation by Roy fielding back in two thousand.
And his insight really was that the world wide web is a pretty good model for a distributed sort of network architecture. There's a lot of nice features that HTTP gives us that the traditional remote procedure call more of the sort of soap based style did not. 
So with the loosely coupled rest style we get scalability and we get reliability or just something we'll talk about well ahead. Let's look at some details so obviously we have resources sitting on a server



 And we want to access it from our clients to use a rest full API



We create a URL and that URL identifies which resource on the server we want to access



And then we include one of the HTTP verbs to say what we want to do to that resource



The URL is human readable. Here’s an example so here we're trying to retrieve see the get verb on the front product with an ID of two



And that style right there is one of the most common ones. But rest really isn't prescriptive is more of some architectural guidance.So the details can vary from server to server so for example it's possible that the server you're talking to uses that style with a query string instead of the slash ID that we saw a moment ago.So just check the documentation for the service that you're working with



Look at in just a little more detail at the different operations so the verb get that is used to retrieve a resource



And we'll get back the resource .In this case a product as the body in the response



If we want to create a new resource, we use post



So we serialize the object include it in the body of the post



and then the server will create the resource in its own data store



It will send back to us. Well this is another thing that can vary by server. It’ll contain either the new resource that just got created or maybe just an ID or URL in case we want to retrieve that same resource again later



And use put to update an existing resource



And again you would include the new data as part of your request



and finally use delete to remove a resource



There are other HTTP verbs that we're not going to discuss like optionhead and so on. There are some rest services that support these but they're really uncommon. Rest uses the regular HTTP response codes to tell you whether or not the request succeeded or failed . So for example you might get back ok ,not found and so on. So it's probably good idea check those when you get back your response .



Rest also uses the content type flag to tell you the format of the data that you're receiving here we have text xml but you might also get something like application slash Json. Json probably would be more common these days



And then of course in the body is the actual data that you requested. Here is the XML example and one kind of interesting thing about rest is… it’s pretty lightweight there is no extra envelope surrounding the response like there would be in something like soap



 Next we're going to talk about a really nice performance issue. Some of the operations that you do with a server change the data on the server and some don't. Those that don't are called safe so get options in head or considered safe operations so they don't change the server



In the safe operations the results can be cashed obviously you have a cast in the middle and there's different people that would do the caching probably your network provider or your cellular network provider in the case of mobile.



So the first time you call a safe method you have to go all the way to the server of course

 But the response can get cached and then of course sent back to the client



The next time you make that same request you get a response back immediately from the cache without going all the way to the server



 So this is one of the things that gives rest-based service of their scalability and performance.
 Another nice property is that some of the HTTP verbs are idempotent operations and what that means is. If you send off the same request so two requests containing the same data .The two requests or more requests are going to yield the same result on the server. And this is actually a little bit tricky to see .So let's look at a quick example here so these operations are idempotent



and it's probably not surprising that get is one of them
For example if you sent that request to the server, multiple times the state of the server would be the same after each one of those calls. Therefore, that is pretty intuitive.



 I think here's the one that's not so intuitive delete is actually idempotent so what that means is the first time I send this off that's the resulting state of the server after the operation has finished.



If I send that same request again the state of the server won't change. The item in question has already been deleted. This property does not require that the response is the same each time. For example the second time we sent that request it's perfectly fine that we receive a not found because the record doesn't exist the second time.




So the reason you care about this is because idempotent operations can be sent multiple times which means that you can resend a request multiple times without worrying about issues like side effects.

Let's finish off with just a couple of quick guidelines generally these days JSON is recommended mostly because


·          It's smaller and therefore it's more performant.
·          Check your result codes.
·          Resend requests when they are idempotent and safe if you get a timeout.
·          Check the server documentation just to make sure that you understand something like the URL format. Not every service that looks at first like a restful service actually is. There some out there that don't follow many of the rest guidelines so read the documentation.
·          Rest does not include any security specification so it's really up to the server and you as the client are just going to have to use whatever the server requires of you typically these days that'll be OAuth2.

·          And finally prefer secure connections HTTPS to protect your data as it goes across the wire.

18 comments:


  1. Bams Lahore is the Best Development Company in Lahore. We Provide Completely Customized Web Solutions for your Organizations different Web Requirements
    web development companies in lahore
    web development services

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. wow this saintly however ,I love your enter plus nice pics might be part personss negative love being defrent mind total poeple , a cool way to improve

    ReplyDelete
  4. Nice blog! I really loved reading through this Blog... Thanks for sharing such a very interesting post with us and keep blogging. Visit our website-
    Xamarin App Developers
    ABP .io Development company
    Web and App development company

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Thanks a lot for sharing this informative blog with us ,it wii be very beneficial for everyone .if anyone want to develop their game please contact us and visit our websites https://www.techgropse.com/mobile-app-development-riyadh-saudi-arabia

    ReplyDelete
  7. This article is indeed very helpful and insightful, thanks for the information shared.
    Web App Develompent

    ReplyDelete
  8. It was not first article by this author as I always found him as a talented author. Goku Drip Jacket

    ReplyDelete
  9. One of the key advantages of hiring SEO Services in Jaipur is the expertise and experience these professionals bring to the table. They have an in-depth understanding of search engine algorithms and stay updated with the latest trends in the industry. With their knowledge and skills, they can devise customized SEO strategies that align with your business goals and yield long-term results.

    ReplyDelete
  10. Informative blog and knowledgeable content. Keep sharing more.

    ReplyDelete
  11. M AND M LOGICO SOLUTIONS FOR SUV AND SEDAN Lincoln mkt Sedan
    Step into the luxury world as you board a journey in our Lincoln MKT Sedan. Whether it’s business trips, corporate events, or airport travel, these vehicles represent the peak of luxury travel. Every detail of our SUVs reflects a commitment to providing an extraordinary travel experience, from the sleek exterior to the lavish interior.

    ReplyDelete
  12. As we continue to advance in API technology, understanding these differences will be crucial for selecting the right approach for your projects. supermicro distributor uae

    ReplyDelete