Getting Started with Refit in .NET

Recap of Refit

Refit is the automatic type-safe REST library for .NET Core, Xamarin and .NET.

It lets you define your API calls using C# interfaces in simple three steps.

Getting Started with Refit in .NET

That is not the target for today’s newsletter, if you are not aware about how to use Refit in .NET then check this article I wrote earlier :

Using Refit to Consume APIs in .NET Core

πŸ‘‰ What Are Query Attributes and How to Add Them?

When you pass parameters in the query string of a URL, the parameters are appended to the URL after a question mark (?), and each parameter is specified as a key-value pair. Multiple parameters are separated by an ampersand (&). The general syntax is as follows:

http://example.com/path/to/resource?parameter1=value1

We can achieve similar in refit like this : Getting Started with Refit in .NET

Parameter with [Query] would be passed as a query string and the one in command would be passed in body.

If you wish to add multiple parameters then add them and just add [Query] before that.

πŸ‘‰ How to Properly Set the Content Type Header

Content headers are essential, we need to set them with each request. Let me show you how would I add that.

I prefer to make a delegating handler for it, here you can read about it :

HTTP Message Handlers in ASP.NET Web API

That is how I would add default header : Getting Started with Refit in .NET

πŸ‘‰ How to Add Bearer Token in Headers

Similarly I can create a delegating handler for adding bearer token. Getting Started with Refit in .NET

We can register it similarly as we did with content header. Add this handler only with your secure APIs which need token for successful call.

This article was originally published at https://mwaseemzakir.substack.com/ on .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place