Тёмный

Using HttpClient in .NET Core to Connect to APIs in C# 

IAmTimCorey
Подписаться 421 тыс.
Просмотров 190 тыс.
50% 1

Опубликовано:

 

29 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 390   
@zoltan.halasz
@zoltan.halasz 4 года назад
This is my favorite kind of tutorial video. Focused, not too long, sample code. Thank you.
@IAmTimCorey
@IAmTimCorey 4 года назад
You are welcome.
@HeathInHeath
@HeathInHeath Год назад
BTW, this is a truly valuable lesson. I really learned a lot about the theory as well as the nuts and bolts of getting it working. Thanks.
@IAmTimCorey
@IAmTimCorey Год назад
Glad it was helpful!
@matthewlevis4436
@matthewlevis4436 4 года назад
Would be good to see how authorization would be applied to the API Client() as well e.g. Auth0
@IAmTimCorey
@IAmTimCorey 4 года назад
I will add it to the list. Thanks for the suggestion.
@HungTran-jx2xc
@HungTran-jx2xc 4 года назад
@@IAmTimCorey Thank Tim, I'm looking forward this topic.
@sunnyokoro
@sunnyokoro 4 года назад
I think it would be added after creating the client, that is at the point of usage.
@snitkjaer
@snitkjaer 3 года назад
Agree adding httpClient auth token handling would be great!
@russellkemmit73
@russellkemmit73 3 года назад
@@IAmTimCorey Would love to see this TIm
@dakathulatris728
@dakathulatris728 4 года назад
I have a technical interview next week, youre helping me so much! Thank you!
@IAmTimCorey
@IAmTimCorey 4 года назад
Awesome!
@dreddy5187
@dreddy5187 2 года назад
Can you extract the api call and model creation into a service class to clean up this code and make it more reusable?
@kourosh234
@kourosh234 2 года назад
Very nice video thank you, I just wished you had gone further to post and bearer authentication
@IAmTimCorey
@IAmTimCorey 2 года назад
I do that in the TimCo Retail Manager course here on RU-vid, if you are interested.
@bassonette
@bassonette 3 года назад
Can you go over how to add basic authentication to the client ?
@IAmTimCorey
@IAmTimCorey 3 года назад
ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-9QU_y7-VsC8.html - check this out
@cbirchy87
@cbirchy87 4 года назад
Great help! I could mind a video on api with auth and sending bodies etc. Also some SignalR would be useful. Thanks Tim :)
@IAmTimCorey
@IAmTimCorey 4 года назад
I will add it to the list. Thanks for the suggestion.
@H8orSk8
@H8orSk8 4 года назад
Would it be better in terms of clean architecture to have a separate service class that calls the api? or its it better off in the blazor component file itself
@IAmTimCorey
@IAmTimCorey 4 года назад
Yes, if the app has any size at all, you should move the API calls out into the data access layer.
@H8orSk8
@H8orSk8 4 года назад
@@IAmTimCorey Thank you for the advice, always watching your videos and your great content!
@AnaHelena
@AnaHelena 4 года назад
Great video, Tim! Thanks a lot
@IAmTimCorey
@IAmTimCorey 4 года назад
Thank you!
@sunnyokoro
@sunnyokoro 4 года назад
this was very helpful. Thanks for sharing
@IAmTimCorey
@IAmTimCorey 4 года назад
Glad it was helpful!
@stewarthyde5111
@stewarthyde5111 4 года назад
Tim, A lot of things have change with Webasembly especially in this area. I pretty sure this can be done ASP.Core Host server, it much more different related to HtmlClient. I want this work in something I am working - my quests it probably related to client is not actually a core project. When you research Webassembly, please research HtmlClient example here and with that project. One big difference there is no startup.cs
@stewarthyde5111
@stewarthyde5111 4 года назад
Base on Microsoft docs - I try the following replacement in program.cs string uri = "www.metaweather.com/api/"; var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add("app"); builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); builder.Services.AddHttpClient("ServerAPI", client => client.BaseAddress = new Uri(uri)) .AddHttpMessageHandler(); builder.Services.AddTransient(sp => sp.GetRequiredService() .CreateClient("ServerAPI")); await builder.Build().RunAsync();
@IAmTimCorey
@IAmTimCorey 4 года назад
I will be showing how to do this in Blazor WebAssembly shortly.
@easterdays4208
@easterdays4208 4 года назад
In this case I'm using third party library like restsharp. Btw thanks for sharing
@IAmTimCorey
@IAmTimCorey 4 года назад
You are welcome.
@martinchristiansen556
@martinchristiansen556 4 года назад
I need to consume an endpoint that returns a JSON array (that is, the JSON text starts with a '['-character). Generating the model from JSON with "Paste Special" works fine. However, because the received JSON represents an array and not a single object, I think that "Paste Special" is doing it wrong when it generates a *single* Rootobject (with an array inside of it). And obviously it doesn't work either, because the *ReadFromJsonAsync()* throws an exception on the first JSON character it sees (the '['). Is there a trick that I can use to consume the JSON anyway?
@martinchristiansen556
@martinchristiansen556 4 года назад
I think I found the answer myself: Just disregard the genereted *RootObject* class and do a *ReadFromJasonAsync()* instead (where *Class1* is the generated type of the objects inside the JSON array)
@IAmTimCorey
@IAmTimCorey 4 года назад
Since it is sending back an array, you just ask for an array of the RootObject, yes.
@DoctorDoomsPvP
@DoctorDoomsPvP 3 года назад
@@martinchristiansen556 Thanks, this might fix the issue I'm having too. I was trying to pull in a JSON RootObject that contains a list of objects but I guess I just need to read directly into the list.
@nisimkd1
@nisimkd1 3 года назад
Hi Tim, great video, thank you! What do you think about using the Nuget package Newtonsoft.Json in the .net core project?
@IAmTimCorey
@IAmTimCorey 3 года назад
The new System.Text.Json package is faster, so if you are able to use it and don't need the more advanced features of Newtonsoft, I would recommend going that way.
@nisimkd1
@nisimkd1 3 года назад
Thank you, Tim. Where can I see the features of System.Text.Json?
@jaiveerlaghari116
@jaiveerlaghari116 4 года назад
29:37 Hi Tim Great Video again!! Its great help in my journey to learn Blazor and C#. I was trying to see if I can get the 'Title' and 'Timezone' but not able to figure it out...
@IAmTimCorey
@IAmTimCorey 4 года назад
Are you saying those two properties are empty or that you don't know how to display them? If the former, check the spelling of the properties. If the latter, follow the pattern of what I did for the other properties.
@justinpowell1220
@justinpowell1220 Год назад
How has this changed for ASP.NET Core 7.0?
@pylvr8021
@pylvr8021 3 года назад
any ideas how can i mock the library to unit test my code ?
@IAmTimCorey
@IAmTimCorey 3 года назад
I recommend a couple of my videos for this - ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-DwbYxP-etMY.html and ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-ub3P8c87cwk.html
@dylanlivera
@dylanlivera 4 года назад
Hi Tim, Hope you are well. Is it possible to have a video what happening behind during dependency injection. If I'm not wrong I think there are so many things we can do using dependency injection while configuring services. I would love to learn more about that. Thanks.
@IAmTimCorey
@IAmTimCorey 4 года назад
I'll add that to the list. I did cover DI in this video: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-mCUNrRtVVWY.html
@dylanlivera
@dylanlivera 4 года назад
@@IAmTimCorey Thanks for the reply Tim. I just started watching your videos. I will definitely take a look at that video. Great content.
@S3Kglitches
@S3Kglitches Год назад
What strategy do you suggest for clients generated by NSwag which is default for scaffolding REST API Clients in NET Framework in Visual Studio? It accepts HttpClient as an argument. Do you suggest creating one such client everytime (short lived, such as DbContext with EF Core) and populating it with a HttpClient received from IHttpClientFactory? The problem is that the NSwag-generated client accepting HttpClient in constructor does not implement IDisposable. How to work with that?
@gugildak12
@gugildak12 Год назад
I was searching for this topic for a long time thanks. Can i create a Web scraper api ?
@IAmTimCorey
@IAmTimCorey Год назад
Sure.
@WorstDeveloper
@WorstDeveloper 4 года назад
Is there any difference in terms of performance when using try catch?
@IAmTimCorey
@IAmTimCorey 4 года назад
Try/catch does add overhead but unless you are making hundreds of repeated calls per client, it should not be noticeable. Good question.
@jeyaprakash6985
@jeyaprakash6985 3 года назад
Hi Tim, When we use Multiple named http client, is there any way we can assign the common properties in single place instead repeating in all the named http clients? Thanks in advance
@bradweir3085
@bradweir3085 2 года назад
rate limiting requests to remote api so I don't get banned from nominatim...
@eeevans
@eeevans 4 года назад
Hi Tim, I tried this except using the WebAssembly project and ran into CORS issues that I haven't been able to resolve. I'm not sure where the issue needs to be corrected. I ran the source linked above and it does work for me so it seems to be a webassembly specific issue.
@IAmTimCorey
@IAmTimCorey 4 года назад
I'm going to be covering how to work with CORS very soon.
@eeevans
@eeevans 4 года назад
IAmTimCorey please do it in the context of web assembly as it is fairly well covered for server.
@GabrieGliukaz
@GabrieGliukaz Год назад
Too much non HttpClient related noise.
@gofudgeyourselves9024
@gofudgeyourselves9024 4 года назад
What are your thoughts on react native for windows and macos from Microsoft. Will I kill native development
@anpham871
@anpham871 4 года назад
Everyone hates PHP. Do you think it's dead?
@IAmTimCorey
@IAmTimCorey 4 года назад
Nope. It is an option but it is not going to become the only option. In general, you will not find any languages or systems that "kill" all of the rest. There are just too many scenarios for one solution to fit well.
@magnus7538
@magnus7538 4 года назад
That Paste Special magic blew my mind!
@IAmTimCorey
@IAmTimCorey 4 года назад
Pretty cool, huh?
@JoeBonez
@JoeBonez 4 года назад
Ditto! Completely new to me.
@Max_Jacoby
@Max_Jacoby 4 года назад
Ikr, my jaw was dropped when I saw this.
@melleck81
@melleck81 4 года назад
I can't find it in VS for Mac?
@adrianspikes6454
@adrianspikes6454 2 года назад
Hi Tim, would this method work for calling an API that is a completely separate app? From debugging the code I have noticed that my "client" is NULL therefore I believe my Blazor App is not finding the URI that is supplied! Tried several different techniques but to no avail. I have looked thru MS documentation, watched this video several times while taking notes and viewed videos from Nick Chapass /Raw Coding but neither are using separate apps. Any help from yourself or other pupils would be appreciated. Thanks.
@dheerajthodupunoori7431
@dheerajthodupunoori7431 4 года назад
would be good , if you have some video on HttpClient(POST) , with some advanced concepts like sending IFormFile in post request , send parameters to post request
@IAmTimCorey
@IAmTimCorey 4 года назад
I will add it to the list. Thanks for the suggestion.
@bavman2130
@bavman2130 4 года назад
Thanks so much Tim, you have a way of explaining things that make it easy to understand and gives context. Much appreciated!
@IAmTimCorey
@IAmTimCorey 4 года назад
My pleasure!
@philibertande3097
@philibertande3097 Год назад
Hi Tim, I have a very important question. The problem with IHttpclientFactory is that when used for a service like in blazor, it always creates a Transient, Service. But most of the time we want the service to be Scoped as an example to make it clearer. With Httpclient we write for a scoped service in program.cs of the client.WASM in NET6. builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); builder.Services.AddScoped(); which is scoped fine. But with IHttpclientFactory we would have builder.Services.AddHttpClient(client => { client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress); }); Which is transient which is not what we want! we want it scoped when injected in a component. How can we make this IHttpclientFactory create a scoped service here for (IProductService, ProductService)!!? The' easiest way possible?!! Thank you for your help.
@IAmTimCorey
@IAmTimCorey Год назад
You might need to stick with HttpClient for that (see the docs here: learn.microsoft.com/en-us/dotnet/core/extensions/httpclient-factory#httpclient-lifetime-management ). If that doesn't explain it well enough, you probably need to ask this question on Stack Overflow.
@saadaldulaijan4957
@saadaldulaijan4957 4 года назад
Thank you tim for the great content, past as json is really great 👍🏻
@IAmTimCorey
@IAmTimCorey 4 года назад
You are welcome.
@paulpieniadz1002
@paulpieniadz1002 4 года назад
How do you deal with making API calls to a SQL Server instance hosted locally? I'm using EF Core and EF SQLServer and can get it to work locally when debugging, but for some reason get the 500 Server error when hosting it on IIS and accessing it through a client browser. Any thoughts?
@IAmTimCorey
@IAmTimCorey 4 года назад
I am not sure where in your configuration you have an issue, but something isn't set up right. Maybe your website needs to have the connection string updated. Maybe there is a permissions issue with the username and password in the connection string. Maybe the issue is somewhere else entirely.
@jugghayd
@jugghayd 4 года назад
15:50 "I don't think there's a weather icon" oi-rain is perfect for it!
@IAmTimCorey
@IAmTimCorey 4 года назад
Thanks for sharing.
@tcl78
@tcl78 3 года назад
It is not clear to me why you use System.Net.Http.Json instead of Microsoft.AspNet.WebApi.Client as you did in a previous video on api calls... What's the difference? Why use one instead than the other? Which one should we use?
@IAmTimCorey
@IAmTimCorey 3 года назад
Those are two different libraries. We are using Http.Json to handle translating the data from and to JSON. WebApi.Client does a lot more with connecting to the web. It isn't something we need in this instance.
@xPa7r1cKx
@xPa7r1cKx 4 года назад
Lets assume you have an WebAPI which references Services layer (class library) that handles all the logic. If you inject IHTTPClientFactory to the WebAPI all is fine, but how do you pass the HTTPClientFactory to the service layer so one is able to do calls from there? A class library doesn't know about IHTTPClientFactories and such.
@IAmTimCorey
@IAmTimCorey 4 года назад
It is a NuGet package to add the ability to use IHTTPClientFactory.
@patyue5012
@patyue5012 3 года назад
I wonder what is the difference between WebApi and HttpClient as you have two different videos with different setup. Thank you.
@IAmTimCorey
@IAmTimCorey 3 года назад
WebAPI is the API itself (the project that sends data to anyone who calls it - swapi.dev is an API). HttpClient is the utility in C# to call an API and get the data.
@jamiefones7042
@jamiefones7042 4 года назад
Hi Corey! Could you do this tutorial on Blazor Web Assembly? How would this differ?
@IAmTimCorey
@IAmTimCorey 4 года назад
I can add that to the suggestion list.
@vinzer72frie
@vinzer72frie 4 года назад
Wow I didn't know that paste json classes its pretty useful for third party api
@IAmTimCorey
@IAmTimCorey 4 года назад
Yeah, that's a nice feature.
@tassarna93
@tassarna93 2 года назад
Hello, i have my solution structured like Client -- Server -- Shared, I have to somehow access the HttpClient on my server project in my Client project to reach IHttpClientFactory, anyway to resolve this?
@IAmTimCorey
@IAmTimCorey 2 года назад
Those are separate projects. You cannot access a class instance across those projects. You will need to create a IHttpClientFactory on your client project as well, not just on your server project.
@tassarna93
@tassarna93 2 года назад
@@IAmTimCorey Awesome, thanks!
@russellkemmit73
@russellkemmit73 3 года назад
I think for learning sake, a POST example would have worked great here.
@IAmTimCorey
@IAmTimCorey 3 года назад
Thanks for the suggestion.
@wmd5645
@wmd5645 4 года назад
Im really wishing my university would have been teaching this stuff 4 years ago. MS VS is so nice.
@IAmTimCorey
@IAmTimCorey 4 года назад
It is a great IDE.
@mrcalico7091
@mrcalico7091 4 года назад
They were teaching us Ada & Fortran at USC back in the 80s when the industry was using C & Visual Basic! Thank god, I was working in the industry more than going to class back then. I'm a big believer of learning computer science and software development online instead of in schools.
@alexanderkorn2009
@alexanderkorn2009 4 года назад
@@mrcalico7091 Feels like it's now happening as the trend that will change the surface of an education methods dramatically. Those days when online courses have been looking just like the way to spend time are almost gone.
@ZoidbergForPresident
@ZoidbergForPresident 4 года назад
@@mrcalico7091 Not everyway is for everyone, one way or the other. ;)
@galaxiegalaxie5003
@galaxiegalaxie5003 4 года назад
Thanks,could you also do the video about refit with dot net coreand blazor webassembly,best practises😁
@IAmTimCorey
@IAmTimCorey 4 года назад
I will add it to the list. Thanks for the suggestion.
@sunilgv4750
@sunilgv4750 3 года назад
Wove great. Can you please create a training session on creating a api gateway in .net core?
@IAmTimCorey
@IAmTimCorey 3 года назад
I will add it to the list. Thanks for the suggestion.
@williamsworkshop8624
@williamsworkshop8624 4 года назад
Another great video! Even as a relatively experienced dev, there's always something I'm learning from you. Very helpful.
@IAmTimCorey
@IAmTimCorey 4 года назад
Thank you!
@frankhaugen
@frankhaugen 4 года назад
I recently stumbled upon a library called Flurl which wrappes the HttpClient in a very neat little fluent API
@IAmTimCorey
@IAmTimCorey 4 года назад
Great!
@juanignaciobruzzone2896
@juanignaciobruzzone2896 3 года назад
Thank you for this amazing guide, do you know by any chance how to send a GET request to a blazor page and capture it?
@pilotboba
@pilotboba 4 года назад
Nice info. I think the next step would be to refactor the httpclient code out of the razor page into a weatherService class that does all that work, and inject the weatherService into the razor page to keep your layers and responsibilities de-coupled.
@IAmTimCorey
@IAmTimCorey 4 года назад
Yep, that would be ideal. Get the data access out of the UI for sure.
@facundopugliese9632
@facundopugliese9632 3 года назад
Thanks for the video, Tim. I would like to know how to configure the proxy server to consume the API through it. I'm getting Authentication error in the exception message ( Response status code does not indicate success: 407 (Proxy Authentication Required)). How do that with HttpClient?
@IAmTimCorey
@IAmTimCorey 3 года назад
I don't have a video on that but I'll add it to the suggestion list.
@mrcalico7091
@mrcalico7091 4 года назад
Cool, finally some up-to-date Http client information. Thanks Corey.
@IAmTimCorey
@IAmTimCorey 4 года назад
You are welcome.
@DevJonathanRamos
@DevJonathanRamos 4 года назад
To get properly named json to c# classes you can get them from quicktype.io/csharp/. Then just change the JsonProperty to JsonPropertyName, and add the System.Text.Json.Serialization reference. Thanks for the video Tim.
@IAmTimCorey
@IAmTimCorey 4 года назад
Thanks for sharing.
@torrvic1156
@torrvic1156 9 месяцев назад
Thank you so much sir! That was very important thing to know.
@elisunday1304
@elisunday1304 3 года назад
Paste Special...and my mind was completely blown.
@IAmTimCorey
@IAmTimCorey 3 года назад
Great!
@Funkfreed
@Funkfreed 4 года назад
This is awesome tim. Just one example and you connected us to hundreds of API free for consumption such as spotify or twitter APIs.
@IAmTimCorey
@IAmTimCorey 4 года назад
I am glad it was helpful.
@Noceo
@Noceo 4 года назад
Awesome video, as always. The only thing I do not get, is why you would want to convert Celsius to Fahrenheit 🤔
@IAmTimCorey
@IAmTimCorey 4 года назад
Because the US is backwards. We cling to 32 degrees as freezing, 212 degrees as boiling, 5,280 feet in a mile,12 inches in a foot, etc. Doesn't that sound simpler? 😂
@seacprogrammer8455
@seacprogrammer8455 3 года назад
var client = _clientFactory.CreateClient(); Throws Nasty error 'Value cannot be null. (Parameter 'factory')' Added services.AddHttpClient(); to ConfigureServices SAME ERROR
@TolchinJ
@TolchinJ 3 месяца назад
How do you have a named client and also use a certificate? Without a factory, I would just create a HttpClientHandler and add the client certificate. Then add the handler to the client on creation. Additionally, how would you reset this client to a new certificate without restarting the app?
@jackie4u
@jackie4u Год назад
Thank you for great step by step lesson on HttpClient. I have used couple of time, but the one-liner trick is so great!
@IAmTimCorey
@IAmTimCorey Год назад
You're very welcome!
@varun25aggarwal
@varun25aggarwal 2 года назад
Thanks! I want to create a project for hands-on, but was banging my head on stackoverflow and ms documentations, got this great resource, you are doing a great job.👍
@IAmTimCorey
@IAmTimCorey 2 года назад
Glad it helped!
@ericclaiborne7741
@ericclaiborne7741 4 года назад
Great video, I managed to get my WebAssy app to call API working using this. Definitely had issues with CORS as you mentioned. Also need to see how to send with JWT auth. Also had issue getting config from the program.cs in the WebAssy app (no startup.cs). So I'll be looking forward to another video with a WebAssy app that covers all this. Thanks!
@IAmTimCorey
@IAmTimCorey 4 года назад
Suggested topic noted and added to the list
@ronrondk
@ronrondk 3 года назад
Great video Tim. You should go further and do a video on typed HttpClients and maybe the use of a HttpClientHandler with it.
@IAmTimCorey
@IAmTimCorey 3 года назад
Thanks for the suggestion, added to the list.
@pylvr8021
@pylvr8021 4 года назад
Thanks tim i will watch this later tonight ,Blazor is amazing especially the client version the only thing bugging me is the CSS isolation hope they will add the feature soon, is it possible to share with us the course that you will publish next here on youtube or on your website ?
@saurabhchauhan232
@saurabhchauhan232 4 года назад
This would be really helpful, knowing you have already said in road map video still want to know what will be as free content on youtube
@IAmTimCorey
@IAmTimCorey 4 года назад
I don't have a public roadmap to share at this point but I'm hoping to make some announcements in the next few weeks.
@wayahedia9989
@wayahedia9989 3 года назад
Tim. You're a Godsend. I recently spent a couple of hours writing classes for deserializing large json files. My heart sand when you pulled down the menu to convert json to classes. ! Now I have to write code to deal with multiple different devices with httpclient. I am not using DI , primarily because I don't fully understand it or how it applies to a C# WPF prog like I am developing. This was very helpful. I always love it when something I don't expect pops up in your vids. Thanks again.
@IAmTimCorey
@IAmTimCorey 3 года назад
I am glad it was helpful.
@Ristogod
@Ristogod 4 года назад
I noticed you didn't dispose the HttpRequestMessage or HttpResponseMessage. Was that intentional? Is there no benefit or drawback in doing so or not?
@IAmTimCorey
@IAmTimCorey 4 года назад
Correct. They get disposed of like any other object in C#.
@Ristogod
@Ristogod 4 года назад
@@IAmTimCorey Sure. But they implement IDisposable. So, just confused how a person knows when and when not to declare them with a using statement.
@brianpk7206
@brianpk7206 2 года назад
@@Ristogod Pretty sure you should be using a using statement. But hey I'm no professional
@tomabbott9221
@tomabbott9221 4 года назад
Is there an elegant way of adding query string parameters to attach to the GET request, or is it best to build the full URL manually with something like stringbuilder?
@IAmTimCorey
@IAmTimCorey 4 года назад
Probably the easiest is just appending the parameters. No need for even stringbuilder since you aren't doing it repetitively.
@conanfoster3870
@conanfoster3870 Год назад
metaweather api is no longer working.
@IAmTimCorey
@IAmTimCorey Год назад
Bummer. You can switch over to Pirate Weather: pirateweather.net/en/latest/ The API won't be exactly the same, but it will work well.
@harrygarala4119
@harrygarala4119 Год назад
Is there any idea to without change in web api & only change in wasm to allow cors.
@jonathanvargas9856
@jonathanvargas9856 2 года назад
Thanks a lot! It was great how you went to complex way to simplest way for us to comprehend the whole concepts. I loved it!
@IAmTimCorey
@IAmTimCorey 2 года назад
You are welcome.
@fobiozor19
@fobiozor19 3 года назад
Great tutorial! Could you share how you would decorate a C# property to change it to a C# naming convention?
@IAmTimCorey
@IAmTimCorey 3 года назад
I will add it to the list. Thanks for the suggestion.
@carsugarmodels
@carsugarmodels 2 года назад
AS always, understandable and lovely video!
@IAmTimCorey
@IAmTimCorey 2 года назад
Thanks!
@deltaphilip8611
@deltaphilip8611 2 года назад
This is still relevant in 2022. When I get stuck, I search for one of your videos on the topic. I like your approach.
@IAmTimCorey
@IAmTimCorey 2 года назад
Awesome!
@gamemode_op9536
@gamemode_op9536 Год назад
Hi Tim can you please be so kind and help me with my API? I followed your Video step by step, but for some reason it didn't work. Every time I open the Page, i get the following error message: JsonReaderException: '
@IAmTimCorey
@IAmTimCorey Год назад
You are trying to read a page as JSON but it appears that the page is sending back either XML or HTML. Check the contents of the page you are trying to parse.
@EdilsonAmaral
@EdilsonAmaral 3 года назад
I don't see where the API key and secret come in, How to add it to the body of the request ?
@RomanSuska
@RomanSuska 4 года назад
Thank you for the video Tim. Can you make a video how to use properly HttpClient in a project that has not built-in dependency injection, e.g. Console App, WPF App or Windows Service?
@IAmTimCorey
@IAmTimCorey 4 года назад
Those all have built-in dependency injection in .NET Core (although it is not turned on by default). I can add showing that to my suggestion list.
@WhitbyStuff
@WhitbyStuff 2 года назад
Ah, the joys of RU-vid when you search for how to call an old SOAP webservice in Blazor and you're a way into the suggested video before you realise that's not what's being explained at all and you've wasted time you don't have.
@IAmTimCorey
@IAmTimCorey 2 года назад
Bummer. You could probably take the new ways of talking to HttpClient from this video and combine it with the answer to this Stack Overflow question: stackoverflow.com/a/20108971/733798
@podcastaficionado
@podcastaficionado 3 года назад
Can we get the hostname and or IP address of client PC that acceess the API?
@erajzehra7331
@erajzehra7331 2 года назад
I connect my third party API to web application and the performance is too slow. For that I need to make web service in back-end and call on demand from front end. Can you please guide me on that?
@IAmTimCorey
@IAmTimCorey 2 года назад
That's something you will need to do based upon the training on this channel.
@cenaruthedruid
@cenaruthedruid 4 года назад
That was awesome, I am going to try to use this in some little app tests tonight!
@IAmTimCorey
@IAmTimCorey 4 года назад
Great!
@crnisrb
@crnisrb 4 года назад
Thanks Tim. I wonder how would you overcome that underscore issue in a layered architecture where your DTO or a model would be a part of domain/core and api fetching service a part of Infrastructure layer? Since we can change weather api provider, there should be some mapping involved? Or am I looking at this from the wrong angle? I’d just prefer my models and DTOs to remain POCO.
@IAmTimCorey
@IAmTimCorey 4 года назад
I am not sure I see the issue. If your DTO needs to change based upon a higher layer, you would do a mapping to a new model. Does that answer your question?
@crnisrb
@crnisrb 4 года назад
@@IAmTimCorey Yeah, that's what I figured. What I wanted to ask, for an example in EF, you could decorate your entities, but if you prefer not to, you can specify decoration equivalents in the configuration. That way you can map things outside of the entities and have them remain completely oblivious of EF. IF you then switch to a different ORM, you just (hopefully) write new configuration specific to that ORM. Is there any way to do something similar with JSON deserialization using System.Net.Http.Json ? Hope this clears it up. Thanks, Tim.
@anilpatil4699
@anilpatil4699 3 года назад
The video is very useful but i am getting "The JSON value could not be converted to Domain model" while converting the service response client.GetFromJsonAsync(employeeCache
@nathanjenner
@nathanjenner 5 месяцев назад
Hi, this video was great. Really easy to follow and understand. Thank you.
@IAmTimCorey
@IAmTimCorey 5 месяцев назад
You are welcome.
@yingxing4134
@yingxing4134 3 года назад
Tim, first of all thank you for all the great tutorials! My job schedule is very demanding but I am able to find 20 minutes here and 30 minutes there to follow them. Do you have an ordered index of all the videos? It would be great to go though them in the proper order, right now I am guessing from the names and upload dates.
@SuperDrJeckyl
@SuperDrJeckyl 2 года назад
Just getting around to this one. The Metaweather app doesn't seem to exist anymore. It's not just me, I looked it up and others have the same issue.
@IAmTimCorey
@IAmTimCorey 2 года назад
That’s the unfortunate part of using third party APIs for demos.
@nguyentuananh4078
@nguyentuananh4078 3 года назад
Do you have any video that - Create dotnet core API. - Create dotnet webapp connect to API project Sir?
@IAmTimCorey
@IAmTimCorey 3 года назад
I do in this course: www.iamtimcorey.com/p/getting-started-with-aspnetcore
@vedavathisuda5125
@vedavathisuda5125 3 года назад
Can we consume an webapi in another webapi?? How can we create a request headers with basic authentication and client cert in consuming webapi request??
@IAmTimCorey
@IAmTimCorey 3 года назад
Sure. An API is just a C# application so it can do basically everything any other C# application can do behind the UI. So the calls would be no different than if we were making them from WPF or Blazor Server.
@mikemurphy1824
@mikemurphy1824 2 года назад
Hi Tim, Great Job! I just found your channel and will be a regular from now on. I see in some of the comments there was mention of a video showing how to POST data. Have you posted one of those yet?
@CoderboyPB
@CoderboyPB 2 года назад
In .NET6 there is no longer used to include the Nuget Package, I just found out.
@IAmTimCorey
@IAmTimCorey 2 года назад
I'm not sure what you are saying. Sorry.
@Alibek11221
@Alibek11221 4 года назад
Why don’t u use jetbrains rider it’s much better than vs
@jemsworld
@jemsworld 4 года назад
that's not a free tool and better to demo in an applications that most people use
@IAmTimCorey
@IAmTimCorey 4 года назад
Correct, it is not free. I have also not found the need to use a third-party solution when Visual Studio does everything I need. I often find that people look for "better" tools without first learning how to use the tools they have.
@AngelaAlvarez-jh5ko
@AngelaAlvarez-jh5ko 4 месяца назад
When utilizing HTTPClient, is it actually using HTTPS?
@IAmTimCorey
@IAmTimCorey 4 месяца назад
If your URL is https then yes.
@Cinna54M
@Cinna54M 3 года назад
im assuming a razor page is the equivalent of a combination of a Controller and Webpage from a ASP.NET Core w/ React app?
@glenroyscott7285
@glenroyscott7285 3 года назад
Great tutorial Tim. However how do I add httpclientfactory in a caliburn micro wpf bootstrapper.? The simple container singleton does not allow for the implementation of the client factory.
@TheSveum
@TheSveum 3 года назад
cannot find paste special for paste json as classes
@hfpsc27
@hfpsc27 3 года назад
Great content. Can I get the API config from a data base and kind of "inject" in startup class? Thank You.
@edovanloon
@edovanloon 2 года назад
Hello, I was wondering how to make an input field to make the location variable. I know it's not practical in this case, but as an example
@rainron2664
@rainron2664 3 года назад
Sir tim is it advisable to inject a type client to the page instead of httpclient factory?. the type client implement the httpclient on the constructor. Thank you for these kind of great videos, you motivate me a lot.😊👍☝️
@dougstanley8942
@dougstanley8942 2 года назад
Thanks!
@IAmTimCorey
@IAmTimCorey 2 года назад
Thank you!
@daniellister4701
@daniellister4701 4 года назад
Loving the config file usage, good to know. Also, the one liner for a connection string is fantastic. Thanks again Tim
@IAmTimCorey
@IAmTimCorey 4 года назад
You are welcome.
@masterkodjane5772
@masterkodjane5772 3 года назад
I would like to use HttpClient on Ubuntu server , but that's not working. ( windows only )
@IAmTimCorey
@IAmTimCorey 3 года назад
HttpClient is not Windows-only - it is part of .NET Core and is cross-platform.
@masterkodjane5772
@masterkodjane5772 3 года назад
@@IAmTimCorey can you help me . I receive this error : "The operation was canceled." My code: public async Task PostPaid([FromBody] object ob) { String rep = ""; User d = JsonConvert.DeserializeObject(ob.ToString()); try { var vm = new { username = d.Login, password = d.Pwd }; //WinHttpHandler httpHandler = new WinHttpHandler(); //httpHandler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls; // HttpClient client = new HttpClient(httpHandler); using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Add("Accept", "application/json"); var httpRequestMessage = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri(d.MyUrl), Content = new StringContent(JsonConvert.SerializeObject(vm)) }; httpRequestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage result = await client.SendAsync(httpRequestMessage); rep = await result.Content.ReadAsStringAsync(); } } catch (Exception ex) { return Json(ex.Message); } return Json(rep); }
@JohnMarsing
@JohnMarsing 4 года назад
you talked about doing a Blazor Wasm video, it would be cool if you could do one with Azure Static Web App Preview. This new service is setup for the many common JavaScript frameworks, but Tim Heuer (@TimHeuer) did one with Blazor Wasm. To get live data you would need to create Azure Functions. Anyway thanks for the video
@IAmTimCorey
@IAmTimCorey 4 года назад
I will add it to the list. Thanks for the suggestion.
@mitchell4217
@mitchell4217 4 года назад
Maybe I missed it, but where is the OnInitializedAsync() coming from? I was wondering the same thing, and sure enough, it's underlined on my computer because it can't find the thing to override? Is this a Blazer/Razer only thing?
@IAmTimCorey
@IAmTimCorey 4 года назад
Correct, that is from the Blazor base class.
Далее
The RIGHT Way To Use HttpClient In .NET
11:46
Просмотров 58 тыс.
С какого года вы со мной?
00:13
Просмотров 210 тыс.
titan tvman's plan (skibidi toilet 77)
01:00
Просмотров 6 млн
Don't Use Polly in .NET Directly. Use this instead!
14:58
Introduction to ASP.NET Core MVC in C# plus LOTS of Tips
1:03:56
Forget Controllers and Minimal APIs in .NET!
14:07
Просмотров 69 тыс.
You Are Using HttpClient Wrong
9:06
Просмотров 4,1 тыс.