Make HTTP Requests

Joined
Aug 26, 2023
Messages
14
Programming Experience
Beginner
what is the proper way to create a console app using .net 7 and use http client to hit an api endpoint that requires oauth?
 
Solution
would still have same hurdles of passing in basic auth
Please read the flurl docs on their website; doing auth is essentially a one line operation:

1693252987388.png


Flurl works fluently; you set everything up in one line like "api.com".WithThis(..).AddThat(...).SetOther().Foo().Bar().GetJsonBlah()
sorry - i thought i had the error message in the post. This is the error

Program.cs(41, 10): [CS1061] 'Url' does not contain a definition for 'GetJsonAsync' and no accessible extension method 'GetJsonAsync' accepting a first argument of type 'Url' could be found (are you missing a using directive or an assembly reference?)
 
At the start of the help page it says you have to install the Flurl.Http nuget and add two using statements, one is for string/uri extensions and the other is for http extensions: Fluent HTTP - Flurl
 
Any time you get that "does not contain a definition" and you've copied code from somewhere that asserts it to be correct, you might be missing a using directive or a reference. Some functionality in C# is delivered by tacking it onto existing functions by extension. The library (like Flurl) contains methods that associate themselves with existing things in C#, like a Uri or a string but only if

a) you've imported the library to the project (mostly libraries these days are delvierd via nuget project manager) and added a reference (nuget package manager does it if used but if you got your library elsewhere, you might have to add a reference manually)
b) you've told C# you want to use that library in this file (putting `using ....` at the top of the file does it)

These days, Intellisense in VS tends to add missing `using`s for you, but only if you've imported the library and referenced it, otherwise VS won't know what `using` to add

It seems complicated at first but suffice to say if someone else wrote code (a library) that you want to use then you have to:

1) actually download that code
2) tell c# you want to use it in your project ("reference" it)
3) tell c# you want to use it in this particular file ("using" it)
 
Back
Top Bottom