Use Refit Library to Call Rest Api Endpoint in C# and Asp.net Core

Calling the REST API endpoint to retrieve and update the data is a common requirement of Desktop/Web Application and Refit library makes it's super easy to call the external web service or API.

Refit library can be used in legacy .Net Framework, Xamarin as well .Net Core, and .Net Standard Frameworks. Refit 6 uses System.Text.Json as the default JSON serializer but if someone likes to Newtonsoft.Json as the default serializer then use the Refit.Newtonsoft.Json Nuget package. System.Text.Json is faster and uses less memory but all features are not supported.

Let's see how we can use the Refit library to call external API.

First, install the Refit library in your project through NuGet like below


Once you installed the Refit library you need to create an Interface for defining all the endpoints of the external API which you be calling from your application.

In this example, I am assuming an external Student API to be called from our application.

IStudentClient.cs

using Refit;
using RefitDemo.Model;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace RefitDemo.Client
{
    [Headers("Content-Type: application/json")]
    public interface IStudentClient
    {

        [Get("/api/student/{studentId}")]
        Task<StudentGetStudent(int studentId);
        [Post("/api/student")]
        Task CreateStudent([BodyStudent student);
        [Put("/api/student/{studentId}")]
      Task UpdateStudent([BodyStudent studentint studentId);
        [Get("/api/student")]
        Task<List<Student>> GetAllStudent();
        [Delete("/api/student/{studentId}")]
        Task DeleteStudent(int studentId);
    }
}


As we can see above there are five endpoints in the above code which we will be calling from our caller application and below is the code of all the endpoints in StudentController.

Please note Refit always returns Task or Task<object> from the endpoint so if endpoint Student object then in the return type we need to mention Task<Student>.

As we can see above we have added a common header of Content-Type: application/json for all the endpoints.


We can call the above endpoint like below from StudentService

using Refit;
using RefitDemo.Client;
using RefitDemo.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RefitDemo.Service
{

    public class StudentService
    {
        private readonly IStudentClient studentClient;

        public StudentService()
        {
            studentClient = RestService.For<IStudentClient>("https://localhost:44391");
        }
        public Student GetStudent()
        {
            var student = studentClient.GetStudent(1,token).GetAwaiter().GetResult();
            return student;
        }
    }
}


Assume we have below Student API which we are calling using Refit

StudentController.cs

using Microsoft.AspNetCore.Mvc;
using RefitDemo.Model;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace StudentAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class StudentController : ControllerBase
    {
        [HttpGet("")]
        public async Task<IActionResultGetAllStudent()
        {
            var allStudents = new List<Student>();
            allStudents.Add(new Student
            {
                Firstname = "Rahul",
                LastName = "Verma",
                StudentId = 1
            });


            allStudents.Add(new Student
            {
              Firstname = "Mohammad",
                LastName = "Salim",
                StudentId = 2
            });
            return Ok(allStudents);
        }


        [HttpGet("{studentId}")]
        public async Task<IActionResultGetStudent(int studentId)
        {
            var student = new Student
            {
                StudentId = 1,
                Firstname = "Dave",
                LastName = "Flagg"
            };
            return Ok(student);
        }


        [HttpPost("")]
        public async Task<IActionResultAddStudent([FromBodyStudent student)
        {
            // Use student object to add to the student in DB
            return Ok(student);
        }


        [HttpPut("{studentId}")]
        public async Task<IActionResultUpdateStudent([FromBodyStudent studentint studentId)
        {
          // Use student object to update the student in DB
            if (student.StudentId != studentId)
                return BadRequest("Provided Student doesn't match the record");
            return Ok(student);
        }


        [HttpDelete("{studentId}")]
        public async Task<IActionResultDeleteStudent(int studentId)
        {
            // Use student object to delete the student in DB
            return Ok();
        }
    }
}


Suppose we want to call GetStudent(int studentId) â€‹method from the above Student API and get the result then below is the sample code.

using Refit;
using RefitDemo.Client;
using RefitDemo.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace RefitDemo.Service
{
    public class StudentService
    {
        private readonly IStudentClient studentClient;


        public StudentService()
        {
            studentClient = RestService.For<IStudentClient>("https://localhost:60569");
        }


        public Student GetStudent()
        {
            var student = studentClient.GetStudent(1).GetAwaiter().GetResult();
            return student;
        }
    }
}


As you can see above in the constructor of StudentService we have configured the base URL of the REST API we are calling in our code.

Next, in the GetStudent method, we are calling the GetStudent with StudentId equal to 1.

get_student_result_refit.jpg


Similarly, we can call other endpoints of the API to do multiple operations.

Below is the complete code of how you can call all the endpoints

StudentService.cs

using Refit;
using RefitDemo.Client;
using RefitDemo.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace RefitDemo.Service
{
    public class StudentService
    {
        private readonly IStudentClient studentClient;


        public StudentService()
        {
            studentClient = RestService.For<IStudentClient>("https://localhost:44391");
        }


        public Student GetStudent()
        {
            var student = studentClient.GetStudent(1).GetAwaiter().GetResult();
            return student;
        }


        public List<StudentGetAllStudents()
        {
            var allStudents = studentClient.GetAllStudent().GetAwaiter().GetResult();
            return allStudents;
        }


        public bool AddStudent()
        {
            var student = new Student { StudentId = 3Firstname = "John"LastName = "Demo" };
            studentClient.CreateStudent(student).GetAwaiter().GetResult();
            return true;
        }


        public bool UpdateStudent()
        {
            var student = new Student { StudentId = 2Firstname = "John"LastName = "Demo" };
            studentClient.UpdateStudent(student2).GetAwaiter().GetResult();
            return true;
        }


        public bool DeleteStudent()
        {
            studentClient.DeleteStudent(2).GetAwaiter().GetResult();
            return true;
        }
    }
}


Suppose we want a header for only the specific endpoint then also it's achievable in Refit.

using Refit;
using RefitDemo.Model;
using System.Collections.Generic;
using System.Threading.Tasks;


namespace RefitDemo.Client
{
    [Headers("Content-Type: application/json")]
    public interface IStudentClient
    {
        [Get("/api/student/{studentId}")]
        [Headers("User-Agent:StudentService")]
        Task<StudentGetStudent(int studentId);


        [Post("/api/student")]
        Task CreateStudent([BodyStudent student);
    }
}


Suppose we want to access an external API that requires a token then it's achievable in Refit.

using Refit;
using RefitDemo.Model;
using System.Collections.Generic;
using System.Threading.Tasks;


namespace RefitDemo.Client
{
    [Headers("Content-Type: application/json")]
    public interface IStudentClient
    {
        [Get("/api/student/{studentId}")]
        [Headers("User-Agent:StudentService")]
        Task<StudentGetStudent(int studentId, [Authorize("Bearer")] string token);
    }
}


Calling the above endpoint is achievable like the one below.

using Refit;
using RefitDemo.Client;
using RefitDemo.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace RefitDemo.Service
{
    public class StudentService
    {
        private readonly IStudentClient studentClient;


        public StudentService()
        {
            studentClient = RestService.For<IStudentClient>("https://localhost:44391");
        }


        public Student GetStudent()
        {

            var token = "asfduyfasdfayufsduyafsduyasd";

            var student = studentClient.GetStudent(1,token).GetAwaiter().GetResult();
            return student;
        }
    }
}



Hope you have enjoyed the basics of the Refit library and how to use the same.

Share This Post

Linkedin
Fb Share
Twitter Share
Reddit Share

Support Me

Buy Me A Coffee