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()
In general, you don't if you follow the official specs that OAuth requires some web browser interaction to prove identity. You could use some of the OAuth modes that gives a device access and treat the console program like a device, but that's bending the rules of the intent of OAuth.
 
Ah - sorry I mispoke. The REST API requires basic auth, and I am just wondering what the proper way is to make a GET request to an API endpoint, pass in the username password and return the response

Which I will then use STJ to deserialize into my DTO
 
I may be using improper terminology -
The API endpoint I am wanting to hit is a 3rd party API - I have a valid user name and password that I would like to use to access the API and make a GET request to return data from the api.

I can do it thro postman with the endpoint, and credentials but not sure what proper way in C# is
 
As I recall postman supports several authentication schemes. Which one did you select when you gave your credentials in postman?
 
I did authorization basic auth and input the user name and password

Postman generates this.....but it doesn't look very re-usable
Postman:
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, "https://api");
request.Headers.Add("Authorization", "Basic sldfjasjfasklfjdsjajfjasjfasjajjf");
var content = new StringContent("", null, "text/plain");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
 
Looks correct to me. What makes it unusable for you? I though you said that you know the credentials. If the credentials change, you have postman regenerate and all you need to do is replace line 3.

If you want to be be able to do handle any credentials, then see the first link I set above. It tells you exactly what you need to do: base64 encode the username and password.
 
If the endpoint has an OpenAPI spec then you can generate client code from it using various tools. Several of them have been aggregated under a single VS extension - REST API Client Code Generator for VS 2022 - Visual Studio Marketplace

If you're building your own client, i'd recommend to use Flurl.Http - it makes life very simple - Flurl

For assistance turning the json the service emits into classes, use Convert JSON to Swift, C#, TypeScript, Objective-C, Go, Java, C++ and more<!-- --> • quicktype
 
Hey @cjard - yes that is what I'm after. I am building my own client for consuming the API endpoint.

Is it recommended to use a 3rd party lib like Flurl for consuming the endpoint? Or is it best practice to use just the base http client? Or is it really personal preference? For example, I have seen some code by a Sr Dev who swears by using RestSharp for everything.
 
It depends on your goals and your intent. If you just want to focus on getting the data and don't really care about the mechanics of how things are done, then using a library to provide a level of abstraction is the way to go. If you want full control to be able to monitor and tweak performance (or manage any potential security issues), then writing your own code would be the way to go.
 
So the API will return
"TotalRecords": 16876, - which is the number of total records that my request is returning, and I can only return 1,000 at a time. What is the proper way to "step" or "Iterate" thro all of those API hits until I get all of the data?
 
What you are asking about is called "pagination".

If the API you are calling supports it, then by all means use it. They will appreciate now putting a huge strain on their infrastructure by getting just pages instead of everything in one go.

If the API you are calling does not support it, then you will have to implement code that supports pagination. It is highly recommended that you cache the initial query so that you don't have to keep hitting that 3rd party API.
 
Is there a nuget package or anything already built that auto-handles pagination?

Or is a quick search on my fave engine for C# pagination will return me good results?
 
You can always use the LINQ Skip() and Take() methods if you need to roll your own. If you decide not to implement that caching and just do the brute force of always getting all the data from the source, then you apply the skip and take on those results. But if you implement the caching, then you apply it on your cache.
 
Back
Top Bottom