in c#

Async and RESTSharp

tl;tr; Examples of using Async method of RESTSharp, and comments

As I mentioned last time I was forced to use async architecture because Windows Phone require it. I was newbie in async programming but when I met RESTSharp, it clear everything, I will show you some code block with my comment for it. Basics, and how to connect to Web API check this blogpost

If we want execute GET.

We have to remember that async method is runs in brand new thread! First we have to set REST request, type here your api path, and what method, for this case it will be GET. r.Data is deserialised List of Locations.

If we want execute POST

Here we fill body of our REST request, we add new object which will provide information that we want to POST.

If we want execute DELETE

Now we have to add parameter to URL so our request for example will be “”, and method DELETE

If we want to update (PUT)

In this case we have to provide object which will replace old one, by using of AddObject method. And finally we can execute our request.

Most of you will ask me, how works this part. If we want to execute method ExecuteAsync<T>(). As a first argument we have to provide request. Second argument is CallBack – what we want to do when data will be received, so we can put here some MessageBox, event for logger or simply bind our given data.

In next episode we will be checking, is there any possibility to mix REST a XNA,

  • Bataleon

    Great article! Would you recommend we use async/await with RESTSharp now instead?

    • Pawel Sawicz

      Yes absolutely, right now we can get Task so we can use async/await. If I will have more time I will try to type up something about it.

  • Conall Γ“ GribΓ­n

    Hi Pawel. Thank you for such nice clear explanations. I just wanted to let you know that I found your post via a recommendation on stackoverflow, and i’m very grateful!

    I look forward to reading summore, especially about the hellbreath server. I used to run legend of mir 2 servers as a kid, so similar passion at the time πŸ™‚

    • Pawel Sawicz

      thanks! I will try to write more about helbreath πŸ˜‰

  • markac

    if (r.ResponseStatus == ResponseStatus.Completed)

    This condition is wrong…

    “If there is a network transport error (network is down, failed DNS lookup, etc), RestResponse.ResponseStatus will be set to ResponseStatus.Error, otherwise it will be ResponseStatus.Completed. If an API returns a 404, ResponseStatus will still be Completed”.

    Better use:

    if (r.IsSuccessful)

  • kynao


    I discover both C#, RestSharp and Async πŸ™‚ while i already practice C# and Restsharp (synchronous) and understood what is explained about Async here

    I would like to do with RestSharp the asynchronous way as explained in the article (which i gave the url above). Does someone has a complete simple example like inthe article in the context of RestSharp?

    Does it work to use restsharp without its async features but use async as in the article ? (because i fully understand the async in the article and not yet how to use the restsharp async features).