Intro to Refit REST API Client for C#

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024
  • 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.ap...
    Refit REST Client: reactiveui.git...
    Mailing List: signup.iamtimc...

ความคิดเห็น • 132

  • @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!

  • @Kyogunaik
    @Kyogunaik 3 ปีที่แล้ว +2

    Refit is an awesome tool to have in my toolbox.. thank you for the awesome introductory video 😇

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว +1

      I am glad it was helpful.

  • @p0p0v1988
    @p0p0v1988 3 ปีที่แล้ว +15

    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 3 ปีที่แล้ว +1

      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.

  • @raminguliyev1520
    @raminguliyev1520 3 ปีที่แล้ว +3

    Awesome video, thank you. I have never heard about refit btw

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว +1

      I'm glad I was able to introduce you to something new.

  • @joeprincipato5211
    @joeprincipato5211 2 ปีที่แล้ว

    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  2 ปีที่แล้ว

      Glad it was helpful!

  • @mogalemcbridemokgothu6268
    @mogalemcbridemokgothu6268 3 ปีที่แล้ว +1

    This what I been looking for 🔥🔥 You rock Tim!!!

  • @steviepca
    @steviepca 7 หลายเดือนก่อน

    Great intro to Refit and thanks for the explanation with @onclick="@(e => AddGuest(item))" @35:30 mark

    • @IAmTimCorey
      @IAmTimCorey  7 หลายเดือนก่อน

      You are welcome.

  • @PatricSjoeoe
    @PatricSjoeoe 3 ปีที่แล้ว +1

    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  3 ปีที่แล้ว +3

      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.

  • @nethaber
    @nethaber 3 ปีที่แล้ว +9

    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 3 ปีที่แล้ว

      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

  • @TheJessejunior
    @TheJessejunior 3 ปีที่แล้ว +1

    you are sooo good in explaiinning things!!!

  • @mmurillo09
    @mmurillo09 3 ปีที่แล้ว +1

    This is awesome. Reminds me a bit of the integration wizard for Web Services that comes with VS by default. Thanks for sharing!

  • @NavySturmGewehr
    @NavySturmGewehr 3 ปีที่แล้ว

    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  3 ปีที่แล้ว

      I am glad they have been helpful.

  • @coding_ideas
    @coding_ideas 3 ปีที่แล้ว

    Thank you for this tutorial

  • @ultroai
    @ultroai 3 ปีที่แล้ว

    Excellent tool. Thanks you for another great video. Will use this in my projects.

  • @NAEL4SLR
    @NAEL4SLR 2 ปีที่แล้ว

    Absolutely interesting and helpful tools. Thanks a lot for sharing !

  • @shabanelmogy7912
    @shabanelmogy7912 ปีที่แล้ว

    Excellent Tutorial

  • @digitaldaridesatv1545
    @digitaldaridesatv1545 3 ปีที่แล้ว

    Thank you Tim!
    This video tutorial help a lot make services simple

  • @aand7e4
    @aand7e4 3 ปีที่แล้ว

    Thanks for the tutorial. It will be very interesting to see one about progressive web apps

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว +1

      I will add it to the list. Thanks for the suggestion.

  • @bytechanneltv
    @bytechanneltv 3 ปีที่แล้ว +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

  • @jesusdelarua5995
    @jesusdelarua5995 2 ปีที่แล้ว

    Great course!!!
    Thank you, Thank you, Thank you.

  • @girornsveinsson7970
    @girornsveinsson7970 2 ปีที่แล้ว +1

    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.

  • @lcmarcosr
    @lcmarcosr 2 ปีที่แล้ว +1

    Hi Tim, How can I set The Content-Type to application/json in Refit

  • @delhitepawan5804
    @delhitepawan5804 3 ปีที่แล้ว

    Excellent tuts. Keep it up 👍

  • @willinton06
    @willinton06 3 ปีที่แล้ว +1

    I prefer this way to generating the full api, why would I want 100 classes if i only need a few endpoints?

  • @MarioStaats
    @MarioStaats 3 ปีที่แล้ว

    As always :-) great Video. Great work *ThumbsUp

  • @refugioderisadas
    @refugioderisadas ปีที่แล้ว

    You are amazing!

    • @IAmTimCorey
      @IAmTimCorey  ปีที่แล้ว

      I appreciate the kind words.

  • @uralmb
    @uralmb 3 ปีที่แล้ว

    Just like 'Retrofit' in java & android. Seems nice!

  • @TheOceanLoader
    @TheOceanLoader 3 ปีที่แล้ว

    I LOVE this guy. Dear Tim Corey! What's the story?

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว

      Thanks! As for the story, not sure what story you are wanting to hear.

  • @Layarion
    @Layarion 3 ปีที่แล้ว +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 3 ปีที่แล้ว

      @@kujingo Do you need any interns ?

    • @Layarion
      @Layarion 3 ปีที่แล้ว

      @@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 3 ปีที่แล้ว

      @@kujingo ya let's chat.

    • @emiliowildberger7151
      @emiliowildberger7151 2 ปีที่แล้ว

      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

  • @dotnetdevni
    @dotnetdevni ปีที่แล้ว

    What if u have multiple interfaces must u add a refit client for each one

  • @onurgencay
    @onurgencay 2 ปีที่แล้ว

    Thanks for the video

  • @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.

  • @zensic
    @zensic 2 ปีที่แล้ว

    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  2 ปีที่แล้ว +1

      Interesting. I'll look into it.

  • @WestCoastSwinger
    @WestCoastSwinger 3 ปีที่แล้ว

    Since it's an interface you can also use it when unit testing Blazor.

  • @argeelearner3978
    @argeelearner3978 3 ปีที่แล้ว

    Good stuff, Thanks bro.

  • @Davmalhi
    @Davmalhi 3 ปีที่แล้ว

    Hi Tim, it's a great tutorial I have one question for you how would you unit test this? Many thanks

  • @jpsolares
    @jpsolares 3 ปีที่แล้ว

    Thanks I used for frontend connect to Backend with Identity4.

  • @zeric_
    @zeric_ 3 ปีที่แล้ว

    Refit introduces too much magic that the team isn't fond of

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว

      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).

  • @Tajemniczy
    @Tajemniczy 3 ปีที่แล้ว +1

    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 3 ปีที่แล้ว +1

      Usually this is done by converting the byte[] to Base64

  • @the-niker
    @the-niker 3 ปีที่แล้ว +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 3 ปีที่แล้ว +1

      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 3 ปีที่แล้ว

      @@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 3 ปีที่แล้ว +1

      You can deal with response status codes if you use Task as return type.

    • @the-niker
      @the-niker 3 ปีที่แล้ว

      @@99MrX99 Sweet, 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.

  • @bradclarke93
    @bradclarke93 9 หลายเดือนก่อน

    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 9 หลายเดือนก่อน

      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

  • @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?

  • @whatgoglikeno6120
    @whatgoglikeno6120 3 ปีที่แล้ว

    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.

  • @YesYou-zy7kp
    @YesYou-zy7kp 3 ปีที่แล้ว

    Have you ever considered doing a series on how to use professional products like Dev Express or Telerik?

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว

      I have but I want to do it right, where it isn't just a sales tool but an actual value.

  • @zensic
    @zensic 2 ปีที่แล้ว

    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  2 ปีที่แล้ว +1

      I'm glad you got it to work.

  • @farsidesc4044
    @farsidesc4044 3 ปีที่แล้ว

    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?

  • @phoniqCZ
    @phoniqCZ 3 ปีที่แล้ว

    Hello, what extension show attribute description (like "path:")?

  • @chrisbordeman107
    @chrisbordeman107 3 ปีที่แล้ว +1

    In the Blazor project, how does it have access to GuestModel?

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว

      Each project has its own copy of GuestModel.

    • @refugioderisadas
      @refugioderisadas ปีที่แล้ว

      @@IAmTimCorey I have a question. If I´m consuming an API where I have a complex json response, how can I map my Models?

  • @sirus49
    @sirus49 3 ปีที่แล้ว

    Is this any better then using the swagger client? Whats the pros and cons to using this over swagger

  • @anveshrocks
    @anveshrocks 6 หลายเดือนก่อน

    How does refit know the right post method on the api controller if there multiple post methods?

    • @IAmTimCorey
      @IAmTimCorey  5 หลายเดือนก่อน

      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.

  • @klearner666
    @klearner666 2 ปีที่แล้ว

    How can we send Files using Refit.?

  • @girornsveinsson7970
    @girornsveinsson7970 3 ปีที่แล้ว

    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  3 ปีที่แล้ว +2

      One of the Fantastic Four (comic book/movie). I just use her name because it is short and easy to remember.

    • @anrodse
      @anrodse 3 ปีที่แล้ว

      @@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: th-cam.com/video/vdhFw1VSowg/w-d-xo.html
      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.

  • @SatyaNagenderGuthula
    @SatyaNagenderGuthula 2 ปีที่แล้ว

    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  2 ปีที่แล้ว

      I'm glad you eventually figured it out.

  • @nanvlad
    @nanvlad 3 ปีที่แล้ว

    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

  • @0387778o
    @0387778o 3 ปีที่แล้ว

    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  3 ปีที่แล้ว

      Yep, they are actually high on my list to cover.

    • @0387778o
      @0387778o 3 ปีที่แล้ว

      @@IAmTimCorey can't wait, thanks.

  • @pralyankar
    @pralyankar 3 ปีที่แล้ว +1

    I am second to watch this :)

  • @DrWambua
    @DrWambua 3 ปีที่แล้ว

    Thank you!

  • @jeffreychong7928
    @jeffreychong7928 2 ปีที่แล้ว

    Can you made a tutorial for Refit on SOAP ?

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +1

      I will add it to the list. Thanks for the suggestion.

  • @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 ปีที่แล้ว +1

      You can enable that in Visual Studio by following this video (I put it at the right time code): th-cam.com/video/f0YeVirKPfw/w-d-xo.html

    • @stevecaruso2965
      @stevecaruso2965 2 ปีที่แล้ว

      @@IAmTimCorey Thanks that is really helpful. And thanks for all the content. I love it.

  • @dotnetdevni
    @dotnetdevni 3 ปีที่แล้ว

    What if ur dealing with a data context and ef multiple models not iust one interface

    • @IAmTimCorey
      @IAmTimCorey  3 ปีที่แล้ว

      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).

  • @knssoftware6018
    @knssoftware6018 3 ปีที่แล้ว

    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  3 ปีที่แล้ว

      It is the same HTTPClient, so no, you don't lose anything.

  • @FellTheSky
    @FellTheSky 2 ปีที่แล้ว

    your older videos had hair in the thumbnail and now its gone.

    • @IAmTimCorey
      @IAmTimCorey  2 ปีที่แล้ว +1

      Just like the hair on my head.

  • @jankool01
    @jankool01 3 ปีที่แล้ว

    gr8

  • @seadahmeti5675
    @seadahmeti5675 3 ปีที่แล้ว +1

    First

  • @chandankumar-kr4vh
    @chandankumar-kr4vh 3 ปีที่แล้ว

    Fourth

  • @seadahmeti5675
    @seadahmeti5675 3 ปีที่แล้ว

    Second

  • @durgaprasadgarapati1748
    @durgaprasadgarapati1748 3 ปีที่แล้ว

    Third