Flurl Documentation

repository·dev·Indexed 26 days ago

https://github.com/tmenier/flurl

A modern, fluent, and asynchronous URL builder and HTTP client library for .NET. Flurl provides a streamlined API for constructing complex URLs and executing HTTP requests, including a built-in HttpTest utility for faking and recording calls during unit tests. Available as a standalone URL builder (Flurl) or a full HTTP client (Flurl.Http), with optional Newtonsoft.Json support via Flurl.Http.Newtonsoft for compatibility and dynamic-returning extension methods.

Tokens
825
Snippets
3
Records
8
Agent score
38%

What's inside Flurl

  1. Install Flurl.Http.Newtonsoft

    dev
    If you are upgrading from Flurl 3.x to 4.x, the default JSON serializer has changed from Newtonsoft.Json to System.Text.Json. To continue using Newtonsoft-based serialization and maintain compatibility with Newtonsoft attributes and settings, install the Flurl.Http.Newtonsoft NuGet package.
  2. Install Flurl via NuGet

    dev

    You can install Flurl using the NuGet Package Manager.

    To install the full HTTP client library (includes URL building and HTTP features), use: Install-Package Flurl.Http

    To install only the standalone URL builder without HTTP features, use: Install-Package Flurl

    PM> Install-Package Flurl.Http
  3. Test HTTP calls with HttpTest

    dev

    Flurl includes HttpTest to allow you to fake and record all HTTP calls made by your code during unit tests. You can use it to arrange responses and assert that specific calls were made with the expected verb and content type.

    [Test]
    public void Can_Create_Person() {
        // fake & record all http calls in the test subject
        using var httpTest = new HttpTest();
    
        // arrange
        httpTest.RespondWith("OK", 200);
    
        // act
        await sut.CreatePersonAsync("Frank", "Reynolds");
            
        // assert
        httpTest.ShouldHaveCalled("http://api.mysite.com/*")
            .WithVerb(HttpMethod.Post)
            .WithContentType("application/json");
    }
  4. Use Flurl for fluent HTTP requests

    dev

    Flurl provides a fluent API for building URLs and making asynchronous HTTP requests. You can chain methods to append path segments, set query parameters, add authentication headers, and send JSON payloads.

    var result = await "https://api.mysite.com"
        .AppendPathSegment("person")
        .SetQueryParams(new { api_key = "xyz" })
        .WithOAuthBearerToken("my_oauth_token")
        .PostJsonAsync(new { first_name = firstName, last_name = lastName })
        .ReceiveJson<T>();