HTTP Get in C# is SUPER Easy! Call REST APIs Like a Pro.

preview_player
Показать описание
#Shorts #dotnet #csharp #.NET #softwaredevelopment

In this concise and informative short video, I show you a very simple way that you can call an API in C# using an HTTP GET request, and receive a payload back (JSON in this example)
Рекомендации по теме
Комментарии
Автор

Nice content! I would suggest making the Main method async in your demos. I understand that this is simple demo code and there's low or no risk of this being an issue, but even in that case I still think it's important to use async/await instead of using the Result property. Thanks for the content!

fredimachadonet
Автор

1. This doesn’t properly dispose of the HttpClient’s socket usage. Either use DependencyInjection with an IHttpClientFactory or simply define a static one to be used globally in your App.xaml.cs file.

2. It is the nature of making calls to the internet that the moment the request leaves the device your app is running on, a myriad of issues could occur — the API could be down, or DNS issues, or even the client’s internet. Considering we can never be 100% sure of our results, it’s best practice to make these calls asynchronously—easily accomplished by changing your method to “public static async Task<T> YourMethodName(T params)” and, adding the “await” keyword in front of your calls and removing “.Result()” from the end of your calls.

3. In the vast majority of situations, people making these HTTP/REST API calls are deserializing to a model. Thankfully, “System.Net.Http.Json” exposes a method for exactly that purpose.

By replacing your code with “return await we’ve increased the stability of your app, decreased its footprint on the client device, added functionality and reduced the overall code written.

IocaIhost