fluent-http | simplest fastest full fledged web server we could come | Runtime Evironment library
kandi X-RAY | fluent-http Summary
Support
Quality
Security
License
Reuse
- Get the routes
- Creates a context out of a list of routes
- Parses annotations for the given resource type
- Compares two UriParser objects
- Executes the request and returns the response body
- Returns true if the type is a list of parts
- Convert path parameters to object type
- Extract the object from the request
- Sets the cookie
- Returns null if not available
- Instantiates watch service
- Returns a session id store
- Generate Google Analytics URL
- Compiles the resources
- Apply the authorization header
- Emit lines
- Creates method annotations factory
- Compiles the given source file
- Return a LessSource for the given filename
- List all the resources in the app folder
- Finds the source file for the given URI
- Compute the ura with the sha1 hash
- Creates the Handlebars
- Starts the router
- Start on a random port
- Handle the HTTP request
fluent-http Key Features
fluent-http Examples and Code Snippets
Trending Discussions on fluent-http
Trending Discussions on fluent-http
QUESTION
Flurl has methods for doing OAuth and Basic authentication:
await url.WithBasicAuth("username", "password").GetJsonAsync();
await url.WithOAuthBearerToken("mytoken").GetJsonAsync();
but how do I do Windows authentication using the currently logged in user? The HttpClientHandler that Flurl is built on top of has a property UseDefaultCredentials but I don't know how to utilize that within Flurl.
var httpClient = new HttpClient(new HttpClientHandler()
{
UseDefaultCredentials = true
});
ANSWER
Answered 2019-Mar-01 at 10:44Flurl intelligently reuses the HttpClientHandler for each domain, so you don't want to set the UseDefaultCredentials each time it runs. Instead, you can modify the HttpClientFactory to return one that's configured to UseDefaultCredentials.
public class UseDefaultCredentialsClientFactory : DefaultHttpClientFactory
{
public override HttpMessageHandler CreateMessageHandler()
{
return new HttpClientHandler { UseDefaultCredentials = true };
}
}
Then you need to tell Flurl to use this factory for the domains you want to use Windows authentication for.
public static class FlurlConfiguration
{
public static void ConfigureDomainForDefaultCredentials(string url)
{
FlurlHttp.ConfigureClient(url, cli =>
cli.Settings.HttpClientFactory = new UseDefaultCredentialsClientFactory());
}
}
Then you simply need to call this once on startup for each domain. For ASP.NET, the Application_Start method in your global application class is a good place for it.
FlurlConfiguration.ConfigureDomainForDefaultCredentials("https://example.com");
FlurlConfiguration.ConfigureDomainForDefaultCredentials("http://services.example.com");
Credit goes to Todd Menier for explaining this to me.
QUESTION
Im making some automatic surveillance for an Rest API im running and i need to retrieve the response body from the HttpResponseMessage object. Im using Flurl Http: https://flurl.dev/docs/fluent-http/
I know how to retrieve the responsebody by adding ".RecieveSomeForm()" at the end of the http request, but i also need to get the response headers, as the error code from the Rest API is sent back as a header. My problem is that - to my knowledge and what i tried - its only the HttpResponseMessage object that i can retrieve the headers from. So the question is: How do i get the responsebody out of the HttpResponseMessage while still being able to retrieve the headers for error logging?
using (var cli = new FlurlClient(URL).EnableCookies())
{
//get response body - var is set to string and has only response body
var AsyncResponse = await cli.WithHeader("some header").Request("some end point").AllowAnyHttpStatus().PostJsonAsync(some body).ReceiveString();
Console.WriteLine(AsyncResponse);
//get headers - var is set to HttpResponseMessage
var AsyncResponse = await cli.WithHeader("some header").Request("some end point").AllowAnyHttpStatus().PostJsonAsync(some body);
Console.WriteLine(AsyncResponse.Headers);
}
ANSWER
Answered 2019-Oct-13 at 17:38If I've understood correctly, you want Headers + Response body from a HTTP response.
var response = await cli.WithHeader("some header").Request("some end point").AllowAnyHttpStatus().PostJsonAsync("some body");
var headers = response.Headers; //do your stuff
var responseBody = response.Content != null ? await response.Content.ReadAsStringAsync() : null;
Another option which I personally don't like:
var responseTask = cli.WithHeader("some header", "haha").Request("some end point").AllowAnyHttpStatus().PostJsonAsync("some body");
var headers = (await responseTask).Headers; //do your stuff
var responseBody = await responseTask.ReceiveString();
Unfortunately, Flurl's extension methods can be used on Task
, not on HttpResponseMessage
. (that's why you have to avoid awaiting in the first line of code)
QUESTION
I am using FlurlHttp and I want to disable AllowAutoRedirect for some API calls. I know How can I get System.Net.Http.HttpClient to not follow 302 redirects?
WebRequestHandler webRequestHandler = new WebRequestHandler();
webRequestHandler.AllowAutoRedirect = false;
HttpClient httpClient = new HttpClient(webRequestHandler);
// Send a request using GetAsync or PostAsync
Task response = httpClient.GetAsync("http://www.google.com")
But for Flurl I found only the way similar to described in C# Flurl - Add WebRequestHandler to FlurlClient (I haven't compiled yet the code below , so it may have some errors)
public class HttpClientFactoryWithWebRequestHandler : DefaultHttpClientFactory
{
private readonly WebRequestHandler _webRequestHandler;
public HttpClientFactoryWithWebRequestHandler (WebRequestHandler webRequestHandler )
{
_webRequestHandler = webRequestHandler ;
}
public override HttpMessageHandler CreateMessageHandler()
{
var handler =_webRequestHandler ;
//Or var handler = new WebRequestHandler(_webRequestHandler );
return handler;
}
}
Then I can pass the setting for a new FlurlClient:
WebRequestHandler webRequestHandler = new WebRequestHandler();
webRequestHandler.AllowAutoRedirect = false;
var fc = new FlurlClient(url)
.ConfigureClient(c => c.HttpClientFactory =
new HttpClientFactoryWithWebRequestHandler (webRequestHandler));
It looks more complicated that it could be. Is it the right way to do or it can be done simplier?
ANSWER
Answered 2017-Oct-09 at 16:25It feels a little heavy because it's a scenario that Flurl doesn't support directly, so it requires tinkering under the hood a bit. You're on the right track but I think there's a few ways you could simplify it. First, I'd suggest creating the WebRequestHandler
inside the factory. Creating it externally and passing it in seems unnecessary.
public class NoRedirectHttpClientFactory : DefaultHttpClientFactory
{
public override HttpMessageHandler CreateMessageHandler()
{
return new WebRequestHandler { AllowAutoRedirect = false };
}
}
If you want this behavior app-wide by default, you could register it globally on startup. Then you don't need to do anything with individual FlurlClient
s.
FlurlHttp.Configure(settings =>
settings.HttpClientFactory = new NoRedirectHttpClientFactory());
Otherwise, if you need the ability to pick and choose which FlurlClient
s you disable it for, an extension method would make it a little easier:
public static IFlurlClient WithoutRedirects(this IFlurlClient fc) {
fc.Settings.HttpClientFactory = new NoRedirectHttpClientFactory();
return fc;
}
Then use it like this:
new FlurlClient(url).WithoutRedirects()...
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install fluent-http
Windows:
Support
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesExplore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits
Save this library and start creating your kit
Share this Page