Consuming a RESTful Web Service in Xamarin Forms using Refit (Part 1)

In Xamarin Forms there are several ways to consume restful web services, could be by using the Microsoft Http Libraries or third party libraries like Refit, RESTSharp, PortableRest, etc.

In this article, I’m going to show you step by step how to interact with a rest api using Refit.

This will be a series of articles covering the following:

  • Part 1: Getting started 
  • Part 2: Structuring your project 
  • Part 3: Structuring your project to consume multiple APIs 

Getting Started

Let’s code Step by Step

1- Install the package

  The first thing we are going to do is to install the Refit Package.

If you are using:

  • Shared project or .NET Standard

  Install the latest version of Refit  (4.0.1 > )

  • PCL

The latest version of Refit (4.0.1) supports .NET Standard, so you will need to install an older version that doesn’t require it, as 2.4.1 or a minor version.

2- Choose an API to consume (Optional)

If you have an API already you can skip this step, but if you just want a test API this article shows some good options https://devlinduldulao.pro/5-public-apis-for-practicing-rest-in-your-xamarin-forms/, for this article will use the second one https://makeup-api.herokuapp.com of this list which basically returns a list of makeup products.

As you can see in the website of this API, the url to consume the makeup list products is http://makeup-api.herokuapp.com/api/v1/products.json?brand=maybelline

Which basically is a GET.

3- Extra project config 

Permissions (Android Only)

In your Android Manifest add the internet permission.

Http cleartext web services (iOS Only)

If you are trying to consume a web service that is a cleartext HTTP, make sure to enable ArbitraryLoads in your Info.plist, check a good reference of how to do it here https://iosdevcenters.blogspot.com/2016/02/transport-security-has-blocked.html.

4-Create an Interface

In our project let’s create a new interface called IMakeUpApi, in this interface, we will add all our API calls, for example:

Let’s analyze what we have here:

In this case, we are going to return a string because we don’t know the structure of the JSON, so we will return a JSON string, in case you know it you can add your object type directly.

Also, as you saw previously the complete URL was http://makeup-api.herokuapp.com/api/v1/products.json?brand=maybelline but I’m just taking the changing part (Relative Url) of this to add it to the interface.

5-Create a method to do the request

As you can see here, I’m using the common part of the URL (Base API URL) as a parameter in the RestService class static method to get an instance that implements our interface internally. After we get this instance we are doing the request by accessing the method we previously defined.

If you put a breakpoint and run your project you will see it returns a JSON with all the products.

6-Create a new model class

We are going to copy this JSON into an online tool to get the C# model class structure, we will use https://jsonutils.com/ (You can use any other tool you want).

Now we are going to create a new class in our project and copy this model.

7-Change the actual returning type (string) for the class type we just created

Note: I’m adding List<MakeUp> instead of just MakeUp because if you check the JSON it is a List.

Now, if you run your project with a breakpoint again you will see the list.  

CONGRATULATIONS!!!

At this point, you have done your first request using refit :), but let’s see what else we can do with it.

– PASSING PARAMETERS 

In Refit there are two ways to pass parameters:

1- Query string

Passing parameters by using query string is really simple, and it can be done dynamically.

For example, as you can see in the URL (http://makeup-api.herokuapp.com/api/v1/products.json?brand=maybelline) of our test API there are some parameters that we can change by in the query string for example brand=maybelline.

So to pass this parameter dynamically using Refit the result would be something like this:

And we will pass the string parameter when doing the request:

2- Body

Passing parameters in request body is almost the same, we can send the request as JSON content and form post.

JSON: We will add a new attribute called [Body] and we will pass the class model (it will deserialize/serialize the JSON to this class internally)

Form post: just add BodySerializationMethod.UrlEncoded

And we will pass the object when doing the request:

Note: This endpoint doesn’t exist in the makeUp API, just made it up to show the concept.

– ADD HEADERS

In Refit there are two ways to add headers:

1- Static

If you have a header that won’t change you can set it static by adding the attribute [Headers] and passing the headers.

2- Dynamic

You will pass it as any another parameter, just make sure to pass the attribute Header next with the header name as value.

And that’s all for now, you can check the full source code here:

https://github.com/CrossGeeks/RefitXamarinFormsSample/tree/Part1-GettingStarted

Happy coding! 🙂

You may also like

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *