FsHttp Documentation

repository·master·Indexed 19 days ago

https://github.com/fsprojects/fshttp

A programmable HTTP client for .NET developers (C#, VB, F#) that provides a readable DSL for common requests and powerful tools for complex HTTP manipulation. It includes a VS Code extension called FsHttp Studio for interactive UI, rich response rendering, and quick metrics.

Tokens
986
Snippets
5
Records
7
Agent score
17%

What's inside FsHttp

  1. Overview of FsHttp

    master
    FsHttp is a 'hackable HTTP client' designed for both interactive playgrounds and production-ready .NET applications (C#, VB, F#). It provides a legible, high-level syntax for common HTTP operations while allowing full access to underlying HTTP representations for complex use cases. It can serve as a programmable alternative to Postman, VSCode's REST client, or .http files.
  2. Use FsHttp Studio in VS Code

    master

    FsHttp Studio is a VS Code extension that enhances the FsHttp experience by providing an interactive UI for F# scripts. Key features include:

    • Run request button: Appears above every http { } block in F# scripts.
    • Rich response rendering: Displays JSON as collapsible trees, renders images inline, and displays HTML as rendered pages.
    • Quick metrics: Shows status code, timing, and response size at a glance.

    You can find it on the VS Code Marketplace.

  3. Build and test FsHttp from source

    master

    To build the project, ensure you have the .NET SDK version specified in ./global.json installed. You can use the F# build script ./build.fsx or the provided bash scripts in the root directory:

    • ./test.sh: Runs all tests. Use --filter Name~'Pattern' to run specific tests.
    • ./docu.sh: Rebuilds the documentation site.
    • ./docu-watch.sh: Rebuilds documentation and watches for changes.
    • ./publish.sh: Publishes all packages (FsHttp and integration packages) to NuGet.
    ./test.sh --filter Name~'Response Decompression'
  4. Use FsHttp for HTTP requests in F#

    master

    FsHttp provides a domain-specific language (DSL) for making HTTP requests in F#. You can use the http computation expression to define requests including the method, URL, headers, and body.

    To use it in an F# script (.fsx) or an F# Interactive (FSI) session, include the NuGet package using #r "nuget: FsHttp" and open the FsHttp namespace.

    #r "nuget: FsHttp"
    
    open FsHttp
    
    http {
        POST "https://reqres.in/api/users"
        CacheControl "no-cache"
        body
        json """
        {
            "name": "morpheus",
            "job": "leader"
        }
        """
    }
  5. Send an HTTP POST request in C#

    master

    In C#, use the Http static class to build requests fluently. Ensure you have the FsHttp NuGet package installed and use await with SendAsync() to execute the request.

    #r "nuget: FsHttp"
    
    using FsHttp;
    
    await Http
        .Post("https://reqres.in/api/users")
        .CacheControl("no-cache")
        .Body()
        .JsonSerialize(new
        {
            name = "morpheus",
            job = "leader"
        })
        .SendAsync();
  6. Send an HTTP POST request in F#

    master

    To use FsHttp in an F# script (FSI), include the NuGet package using the #r "nuget: FsHttp" directive. You can use the http { ... } DSL to define the method, URL, headers, and body, then pipe the result to Request.send.

    #r "nuget: FsHttp"
    
    open FsHttp
    
    http {
        POST "https://reqres.in/api/users"
        CacheControl "no-cache"
        body
        jsonSerialize
            {|
                name = "morpheus"
                job = "leader"
            |}
    }
    |> Request.send
  7. Disable FsHttp debug logs

    master
    By default, FsHttp emits console logs. In F# Scripting (FSI) or non-FSI scenarios where you want to suppress these logs, call FsHttp.Fsi.disableDebugLogs () before making any requests.
    FsHttp.Fsi.disableDebugLogs ()