Intro to Refit REST API Client for C#

  Рет қаралды 49,066

IAmTimCorey

IAmTimCorey

2 жыл бұрын

Calling an API can be a pain. You have to use the HTTPClient correctly, you need to handle the return types, deserialize the data, and more. Or at least, you used to have to do all of that. Not anymore if you use Refit. Refit is an automatic type-safe REST library for Xamarin and .NET. In this video, we are going to set up an API and then call it from Blazor WebAssembly using Refit. You will be amazed at how easy it is to talk to an API.
Full Training Courses: IAmTimCorey.com
Source Code: leadmagnets.app/?Resource=Int...
Refit REST Client: reactiveui.github.io/refit/
Mailing List: signup.iamtimcorey.com/

Пікірлер: 132
@Kyogunaik
@Kyogunaik 2 жыл бұрын
Refit is an awesome tool to have in my toolbox.. thank you for the awesome introductory video 😇
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I am glad it was helpful.
@mogalemcbridemokgothu6268
@mogalemcbridemokgothu6268 2 жыл бұрын
This what I been looking for 🔥🔥 You rock Tim!!!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Great!
@ultroai
@ultroai 2 жыл бұрын
Excellent tool. Thanks you for another great video. Will use this in my projects.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thank you!
@mmurillo09
@mmurillo09 2 жыл бұрын
This is awesome. Reminds me a bit of the integration wizard for Web Services that comes with VS by default. Thanks for sharing!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@NAEL4SLR
@NAEL4SLR Жыл бұрын
Absolutely interesting and helpful tools. Thanks a lot for sharing !
@IAmTimCorey
@IAmTimCorey Жыл бұрын
You are welcome.
@TheJessejunior
@TheJessejunior 2 жыл бұрын
you are sooo good in explaiinning things!!!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks!
@raminguliyev1520
@raminguliyev1520 2 жыл бұрын
Awesome video, thank you. I have never heard about refit btw
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I'm glad I was able to introduce you to something new.
@coding_ideas
@coding_ideas 2 жыл бұрын
Thank you for this tutorial
@tomthelestaff-iamtimcorey7597
@tomthelestaff-iamtimcorey7597 2 жыл бұрын
Thanks for your faith and ongoing support of Tim
@NavySturmGewehr
@NavySturmGewehr 2 жыл бұрын
I'm currently watching one of your other videos on APIs and then I'll do this one! Thank you so much Tim! I've managed to connect to the server and provide my authentication but I'm not quite to getting useful information out of it yet.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I am glad they have been helpful.
@milos5247
@milos5247 Жыл бұрын
Much quicker and easier solution than managing the http client on your own. Don't even want to mention unit testing. Great stuff!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thanks!
@digitaldaridesatv1545
@digitaldaridesatv1545 2 жыл бұрын
Thank you Tim! This video tutorial help a lot make services simple
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@shabanelmogy7912
@shabanelmogy7912 Жыл бұрын
Excellent Tutorial
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Thank you!
@delhitepawan5804
@delhitepawan5804 2 жыл бұрын
Excellent tuts. Keep it up 👍
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks, will do!
@jesusdelarua5995
@jesusdelarua5995 2 жыл бұрын
Great course!!! Thank you, Thank you, Thank you.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@joeprincipato5211
@joeprincipato5211 Жыл бұрын
Great unique video,. I scoured the internet to find an example of creating a web api, using another project in your solution to call the api, but most of all there was no database, just creating lists to use in the Api..
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Glad it was helpful!
@MarioStaats
@MarioStaats 2 жыл бұрын
As always :-) great Video. Great work *ThumbsUp
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks!
@aand7e4
@aand7e4 2 жыл бұрын
Thanks for the tutorial. It will be very interesting to see one about progressive web apps
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I will add it to the list. Thanks for the suggestion.
@argeelearner3978
@argeelearner3978 2 жыл бұрын
Good stuff, Thanks bro.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@steviepca
@steviepca 3 ай бұрын
Great intro to Refit and thanks for the explanation with @onclick="@(e => AddGuest(item))" @35:30 mark
@IAmTimCorey
@IAmTimCorey 3 ай бұрын
You are welcome.
@bytechanneltv
@bytechanneltv 2 жыл бұрын
Hi Tim! Thanks a lot. I`ve only one question: "How should I design the interface if the rest service sends an IActionResult back to caller instead of the awaited object?" Normally on an api call an IActionResult will be send back like Ok(...) or NotFound. THX. Jürgen
@Davmalhi
@Davmalhi 2 жыл бұрын
Hi Tim, it's a great tutorial I have one question for you how would you unit test this? Many thanks
@PatricSjoeoe
@PatricSjoeoe 2 жыл бұрын
Your issue with duplicated id was caused by your count + 1. But if you had used .Max(x => x.Id) + 1 it works :) It's easy to make mistakes in recordings :) Great videos as usual! :)
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
True. It was also due to trying to do it "quick and dirty" on the client-side rather than actually use a database like you normally would. That fix would be the solution for that issue, though. Good catch.
@onurgencay
@onurgencay 2 жыл бұрын
Thanks for the video
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@jainr67
@jainr67 2 жыл бұрын
Thanks for explaining it so well Tim, I have recently used this library to call an API from a client(I am calling it API Client) but now when I want to test the API Client, I don't have the actual Api running with me, and would like to mock it out. but because behind the scene refit creates the httpclient factory, not sure how would I be able to mock it? any idea?
@Tajemniczy
@Tajemniczy 2 жыл бұрын
Big thanks for this tutorial :), easily and brilliantly explained. I have a small questions, and if in the object there are elements of the Image or byte[] type, will there be no problem with downloading such data? i.e. how could I send and retrieve images and byte[]?
@thewhitecrow7472
@thewhitecrow7472 2 жыл бұрын
Usually this is done by converting the byte[] to Base64
@girornsveinsson7970
@girornsveinsson7970 2 жыл бұрын
As this amazing package encapsulates the network request, it is a little bit difficult to see the cause of problems when things do not work. Is it possible to see the content of the body in a Post request? Is there some tooling in Visual Studio that has this? I had problems because the format of a DateTime in the model class was not right for my WebAPI, which I fixed kind of just by guessing that that might be what was wrong, but it would be nice to be able to get more information for troubleshooting.
@farsidesc4044
@farsidesc4044 2 жыл бұрын
I noticed that each of the interface methods start with Task. Is there a case where asynchronous calls would not be preferred? That is to ask, is there a reason why refit wouldn't build in the Task into their attributes?
@Gordeira03
@Gordeira03 Жыл бұрын
You are amazing!
@IAmTimCorey
@IAmTimCorey Жыл бұрын
I appreciate the kind words.
@p0p0v1988
@p0p0v1988 2 жыл бұрын
While Refit reduces the amount of boilerplate code you have to write when integrating with an API to a minimum, and is nice to use for smaller APIs, I think it is more useful to use code generation for the client side, based on OpenAPI/Swagger doc (either from VS or other options like NSwag Studio). That is also very helpful in updating the code when the API is updated, and there is less room for error on the client side.
@noha12011
@noha12011 2 жыл бұрын
Ya beat me to it. Using swagger code generators is a must in big projects. I cant imagine doing all that work to get refit to work on a big project. For reference I've seen generated nswag clients that are 18k lines. While that would be smaller in refit (as you only need the interface) it is also a lot of work by hand where you only need to set up nswag once and just regen any time you (or co-workers, or vendors) make changes to the api.
@DrWambua
@DrWambua 2 жыл бұрын
Thank you!
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You are welcome.
@lcmarcosr
@lcmarcosr 2 жыл бұрын
Hi Tim, How can I set The Content-Type to application/json in Refit
@uralmb
@uralmb 2 жыл бұрын
Just like 'Retrofit' in java & android. Seems nice!
@tomthelestaff-iamtimcorey7597
@tomthelestaff-iamtimcorey7597 2 жыл бұрын
Thanks for spending your time watching.
@pralyankar
@pralyankar 2 жыл бұрын
I am second to watch this :)
@dotnetdevni
@dotnetdevni 8 ай бұрын
What if u have multiple interfaces must u add a refit client for each one
@phoniqCZ
@phoniqCZ 2 жыл бұрын
Hello, what extension show attribute description (like "path:")?
@whatgoglikeno6120
@whatgoglikeno6120 2 жыл бұрын
How does this compare to an autogenerated client from NSwag? I am currently using it in pretty much this setup, an API that I write and a Blazor client that I write. It works, it generates a full client including types and everything. I don't need to do anything. However it is not well documented and can be quite a headache to get working. Once it does, it does look strictly superior though. Especially if you have access to the API. Its a little confusing to newer developers. Creating a Type in the API project, building it, then its "copied" to the Client project. Im thinking maybe sticking to a fully blown NSwag client when I'm also the API Developer, but using this when its an external API. I tried to get NSwag to work with 2 external APIs and failed both times. This looks much easier, since its just route + type. It doesn't even need to be a swagger documented API.
@sirus49
@sirus49 2 жыл бұрын
Is this any better then using the swagger client? Whats the pros and cons to using this over swagger
@the-niker
@the-niker 2 жыл бұрын
Just wondering how do you react to different response status codes differently? Is this possible in Refit at all? Also is there an interface model generator from swagger.json? Hitting update on a generated NSwag client and fixing broken code is easy, going though 50+ classes looking for changes in an API you don't control is not fun. When all you get from a partner API is a swagger.json file and URL you need a generator.
@anrodse
@anrodse 2 жыл бұрын
Why do people is so lucky to get a swagger.json file? I always get very basic documentation in custom websites or (what is worse) PDFs 😔
@the-niker
@the-niker 2 жыл бұрын
@@anrodse Haha, I had to do that once, the endpoint was probably still running DOS and I had to roll the entire thing with just http calls and a lot of imagination. And every even the smallest API error invalidated future calls from the same client. Fun :D
@99MrX99
@99MrX99 2 жыл бұрын
You can deal with response status codes if you use Task as return type.
@the-niker
@the-niker 2 жыл бұрын
@@99MrX99 Sweet, thanks!
@jpsolares
@jpsolares 2 жыл бұрын
Thanks I used for frontend connect to Backend with Identity4.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Great!
@0387778o
@0387778o 2 жыл бұрын
Hey Tim, I don't see that you have covered attributes, I don't understand how they work yet. Will you cover them in the future?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Yep, they are actually high on my list to cover.
@0387778o
@0387778o 2 жыл бұрын
@@IAmTimCorey can't wait, thanks.
@TheOceanLoader
@TheOceanLoader 2 жыл бұрын
I LOVE this guy. Dear Tim Corey! What's the story?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks! As for the story, not sure what story you are wanting to hear.
@zeric_
@zeric_ 2 жыл бұрын
Refit introduces too much magic that the team isn't fond of
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
If you go to their site, you can see exactly what Refit does (and it isn’t a ton - it basically just has us inherit from a base class).
@willinton06
@willinton06 2 жыл бұрын
I prefer this way to generating the full api, why would I want 100 classes if i only need a few endpoints?
@stevecaruso2965
@stevecaruso2965 2 жыл бұрын
In VS I noticed your screen displays Path: next it the [Get] in your interface, is that a Intelliense setup to display that?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
You can enable that in Visual Studio by following this video (I put it at the right time code): kzbin.info/www/bejne/nGG8lomfp7CDnNk
@stevecaruso2965
@stevecaruso2965 2 жыл бұрын
@@IAmTimCorey Thanks that is really helpful. And thanks for all the content. I love it.
@Layarion
@Layarion 2 жыл бұрын
Hey Corey, my dad owns a tent rental buisness (think of large circus tents) and I was thinking I want to build an utility app for android and iOS that allows us to schedule events, keep inventory, and track employee hours. The challenge is I only know c# and have almost zero knowledge and experience outside of simple Unity and Console apps. No web experience or anything beyond standalone desktop apps that don't do more than mess with local files or run a simple game. Where do i even begin? I imagine I'll need to rent a server, but i wouldn't know how to use it.
@paulkeating9259
@paulkeating9259 2 жыл бұрын
@@kujingo Do you need any interns ?
@Layarion
@Layarion 2 жыл бұрын
@@kujingo it does, but I don't have any money - and I don't think my dad is interested in investing into something like that unless it's to support me.
@Layarion
@Layarion 2 жыл бұрын
@@kujingo ya let's chat.
@emiliowildberger7151
@emiliowildberger7151 Жыл бұрын
if it helps you, and if you already know some c#, MAUI is a very good option for mobile and also Blazor to create web applications
@WestCoastSwinger
@WestCoastSwinger 2 жыл бұрын
Since it's an interface you can also use it when unit testing Blazor.
@tomthelestaff-iamtimcorey7597
@tomthelestaff-iamtimcorey7597 2 жыл бұрын
Thanks for the tip
@YesYou-zy7kp
@YesYou-zy7kp 2 жыл бұрын
Have you ever considered doing a series on how to use professional products like Dev Express or Telerik?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I have but I want to do it right, where it isn't just a sales tool but an actual value.
@zensic
@zensic Жыл бұрын
I'm not sure why. But the post request doesn't work for me I got an 400 error (.NET 6, I enabled cors on both API server and client)
@IAmTimCorey
@IAmTimCorey Жыл бұрын
I'm glad you got it to work.
@bradclarke93
@bradclarke93 6 ай бұрын
Struggling with more complex JSON that has a child object in it, for example attributes :{call time:10,calls:20}. On a POST body. The refit doesn’t seem to handle it well, any good example of how to deal with it
@bradclarke93
@bradclarke93 6 ай бұрын
made some progress , but still need to figure out how to log the BODY of the POST, as something is not quite right. Logging /debugs with REFIT seems to be mystery in my searching
@anveshrocks
@anveshrocks 2 ай бұрын
How does refit know the right post method on the api controller if there multiple post methods?
@IAmTimCorey
@IAmTimCorey 2 ай бұрын
Each post method has its own URL. You can't have two POST methods use the same URL. So, you just tell Refit which URL you are calling and it does the rest.
@girornsveinsson7970
@girornsveinsson7970 2 жыл бұрын
Thanks a lot. This looks great as it simplifies things quite a bit. But I have been wondering for years... Who is Sue Storm??? ;-)
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
One of the Fantastic Four (comic book/movie). I just use her name because it is short and easy to remember.
@anrodse
@anrodse 2 жыл бұрын
@@IAmTimCorey I always thought she was someone of your family 😅
@bradclarke93
@bradclarke93 2 жыл бұрын
Where would I do the cors in .net6 vs .net5 , it looks like there is no startup.cs now (rookie)
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
This video should help: kzbin.info/www/bejne/rJXLd6pni7iirck And don't worry about being a rookie. We all are when it comes to .NET 6 changes. None of us have even 6 months of experience with the production version of .NET 6 yet.
@chrisbordeman107
@chrisbordeman107 2 жыл бұрын
In the Blazor project, how does it have access to GuestModel?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Each project has its own copy of GuestModel.
@Gordeira03
@Gordeira03 Жыл бұрын
@@IAmTimCorey I have a question. If I´m consuming an API where I have a complex json response, how can I map my Models?
@arumteguh2762
@arumteguh2762 2 жыл бұрын
onclick="@(e => AddGuest(item))">Add Guest onclick="@(e => UpdateGuest(item))">Update Guest onclick="@(e => DeleteGuest(item))">Delete Guest I follow step by step of this tutorial and this onclick code give me this error "Cannot convert lambda expression to type "object" because it is not a delegate type". any idea how to fix it? I already searching but cannot find a clue or the solution.
@SatyaNagenderGuthula
@SatyaNagenderGuthula Жыл бұрын
I had a strange issue which I fixed, While creating the Interface I had copied the relative path for endpoints from swagger and build my client. But when I execute it I get 404 for the same endpoint which I can execute through swagger. The API url generated by Refit has urlencoded character "%E2%80%8B" - E2 80 8B - "ZERO WIDTH SPACE" values and API is returning 404 for it. After trying a lot, I just recreated the interface and the endpoint by typing in the url and it worked fine.. This wasted half a day of mine.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
I'm glad you eventually figured it out.
@dotnetdevni
@dotnetdevni 2 жыл бұрын
What if ur dealing with a data context and ef multiple models not iust one interface
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
This doesn't really have anything to do with EF. Your API access shouldn't be tied directly to your data access, so how you set up EF would not be affected by your use of Refit (and the reverse is true as well).
@klearner666
@klearner666 Жыл бұрын
How can we send Files using Refit.?
@zensic
@zensic Жыл бұрын
For those who are facing issues with POST and PUT requests, I solved mine by downgrading to version 5 of refit as a temporary fix. Apparently, version 6 implemented a different Json serializer causing requests with a [Body] to not work Another solution I've seen is to add Buffered = True into your RefitSettings, but I still couldn't get it to work
@IAmTimCorey
@IAmTimCorey Жыл бұрын
Interesting. I'll look into it.
@nanvlad
@nanvlad 2 жыл бұрын
Good library but not complete. I expect that I don't have to manually write interface with all methods signatures for api - they must be generated automatically, like in Swagger. All I need is to generate such class and use all methods without any manual api reflection
@jeffreychong7928
@jeffreychong7928 2 жыл бұрын
Can you made a tutorial for Refit on SOAP ?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
I will add it to the list. Thanks for the suggestion.
@knssoftware6018
@knssoftware6018 2 жыл бұрын
When you remove the standard HTTPClient factory for the Refit HTTPClient one, do you loose any functionality or flexibility for working with HTTPClient stuff directly etc, or are the effects only additive, and everything else just works the way it would have?
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
It is the same HTTPClient, so no, you don't lose anything.
@jankool01
@jankool01 2 жыл бұрын
gr8
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Thanks!
@Proviper666
@Proviper666 Жыл бұрын
Good video. But I was hopping for something else. That you will call other API's like "Giphy API" "ricky and morty API" "restcountries API" and show results on swagger.
@IAmTimCorey
@IAmTimCorey Жыл бұрын
I give you the tools. You take them and create what you specifically need.
@seadahmeti5675
@seadahmeti5675 2 жыл бұрын
First
@seadahmeti5675
@seadahmeti5675 2 жыл бұрын
Second
@chandankumar-kr4vh
@chandankumar-kr4vh 2 жыл бұрын
Fourth
@FellTheSky
@FellTheSky 2 жыл бұрын
your older videos had hair in the thumbnail and now its gone.
@IAmTimCorey
@IAmTimCorey 2 жыл бұрын
Just like the hair on my head.
@durgaprasadgarapati1748
@durgaprasadgarapati1748 2 жыл бұрын
Third
@nethaber
@nethaber 2 жыл бұрын
I think that we dont need "Where" in guests.Where(g => g.Id == id).FirstOrDefault(); simpler is guests.FirstOrDefault(g => g.Id == id);
@mvrekbvrszcz
@mvrekbvrszcz 2 жыл бұрын
ReSharper suggest such a change as well, but if I recall correctly there were some videos on the topic and choosing .Where over passing predicate into another LINQ function may bring you performance benefits
Inheritance vs Interfaces in C#: Object Oriented Programming
49:41
IAmTimCorey
Рет қаралды 100 М.
Intro to Redis in C# - Caching Made Easy
1:27:29
IAmTimCorey
Рет қаралды 171 М.
Как быстро замутить ЭлектроСамокат
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 12 МЛН
Китайка и Пчелка 4 серия😂😆
00:19
KITAYKA
Рет қаралды 3 МЛН
How to call a Java REST API using JDK Http Client
22:23
Dan Vega
Рет қаралды 6 М.
What is a REST API?
9:12
IBM Technology
Рет қаралды 1,4 МЛН
Swagger is Going Away in .NET 9!
10:48
Nick Chapsas
Рет қаралды 81 М.
Intro to Benchmark.net - How To Benchmark C# Code
52:15
IAmTimCorey
Рет қаралды 39 М.
API Testing Using Postman | Crash Course
3:27:51
Mukesh otwani
Рет қаралды 75 М.
Женская колония: как тут все устроено?
35:58
Коллектив
Рет қаралды 262 М.
Brutally honest advice for new .NET Web Developers
7:19
Ted's Tech
Рет қаралды 28 М.
Intro to Docker - A Tool Every Developer Should Know
1:16:03
IAmTimCorey
Рет қаралды 207 М.
Как быстро замутить ЭлектроСамокат
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 12 МЛН