Git Product home page Git Product logo

fshttp's Introduction

FsHttp Build & Tests NuGet Badge

logo

FsHttp ("Full Stack HTTP") is a "hackable HTTP client" that offers a legible style for the basics while still affording full access to the underlying HTTP representations for covering unusual cases. It's the best of both worlds: Convenience and Flexibility.

  • Use it as a replacement for .http files, VSCode's REST client, Postman, and other tools as an interactive and programmable playground for HTTP requests.
  • Usable as a production-ready HTTP client for applications powered by .NET (C#, VB, F#).

๐Ÿ‘ Postman? โค๏ธ FsHttp! https://youtu.be/F508wQu7ET0

Developed and maintained by @SchlenkR and @dawedawe. Feel free to leave us a message.

Documentation

F# syntax example

#r "nuget: FsHttp"

open FsHttp

http {
    POST "https://reqres.in/api/users"
    CacheControl "no-cache"
    body
    jsonSerialize
        {|
            name = "morpheus"
            job = "leader"
        |}
}
|> Request.send

C# syntax example

#r "nuget: FsHttp"

using FsHttp;

await Http
    .Post("https://reqres.in/api/users")
    .CacheControl("no-cache")
    .Body()
    .JsonSerialize(new
        {
            name = "morpheus",
            job = "leader"
        }
    )
    .SendAsync();

Release Notes / Migrating to new versions

Building

.Net SDK:

You need to have a recent .NET SDK installed, which is specified in ./global.json.

Build Tasks

There is a F# build script (./build.fsx) that can be used to perform several build tasks from command line.

For common tasks, there are powershell files located in the repo root:

  • ./test.ps1: Runs all tests (sources in ./src/Tests).
    • You can pass args to this task. E.g. for executing only some tests: ./test.ps1 --filter Name~'Response Decompression'
  • ./docu.ps1: Rebuilds the FsHttp documentation site (sources in ./src/docs).
  • ./docu-watch.ps1: Run it if you are working on the documentation sources, and want to see the result in a browser.
  • ./publish.ps1: Publishes all packages (FsHttp and it's integration packages for Newtonsoft and FSharp.Data) to NuGet.
    • Always have a look at ./src/Directory.Build.props and keep the file up-to-date.

Credits

  • Parts of the code were taken from the HTTP utilities of FSharp.Data.
  • Credits to all critics, supporters, contributors, promoters, users, and friends.

fshttp's People

Contributors

b0wter avatar bartelink avatar captncodr avatar davejohnson8080 avatar dawedawe avatar dependabot[bot] avatar drhumlen avatar maciej-izak avatar maxdeg avatar mefgalm avatar michaeloyer avatar nojaf avatar saerosv avatar samuel-dufour avatar schlenkr avatar tnishimura avatar vilinski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fshttp's Issues

toJson hides the original content

When toJson Fails (due to parsing error), the original Content is not shown in the exception so that Debugging is a hard thing to do.

Response content is not rendered until "go" (or others) are used

That smells like a bad sideeffect bug, propably somewhere in config.

#load @"./packages/SchlenkR.FsHttp/lib/netstandard2.0/FsHttp.fsx"

open FsHttp
open FsHttp.DslCE


post "https://reqres.in/api/users" {
    CacheControl "no-cache"
    body
    json """
    {
        "name": "morpheus",
        "job": "leader"
    }
    """
}

Module structuring (Response)

Use

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]

and restructure functions for response handling (no more AutoOpen)

Wrong #I in FsHttp.fsx

The includes don't Point to the correct directories according to the Location in the nuget package.

Additional steps needed for dotnet fsi usage

Following the instructions in
Setup (F# Interactive)
section, but using dotnet fsi (.net core 3.0 preview 5), I get an error when executing a get - specifically ERROR reading response content: System.IO.FileNotFoundException: Could not load file or assembly 'FSharp.Data, Version=3.0.0.0

I needed to do the following to get it working:
> open FSharp.Data;;
> printfn "load module %A" JsonSaveOptions.None;;

After these 2 steps, the get "https://reqres.in/api/users?page=2&delay=3" .> go;; worked without error.

Not sure if you want this added to the instructions, or to wait and see if .net core 3.0 RTM rectifies the issue.

Can't create a http message

I'm using FsHttp to create http messages
This is a breaking change by updating a major version from 3.1.0 to 4.0.2.

let msg =
    httpMsg {
        GET "https://google.com"
    }
This expression was expected to have type
    'FsHttp.Domain.IContext'
but here has type
    'LazyHttpBuilder<FsHttp.Context.HeaderContext>'

I could be wrong, but I have expected such conversion should be in Run, not in Delay as it is right now
https://github.com/ronaldschlenker/FsHttp/blob/master/src/FsHttp/DslCE.fs#L42

Support Proxies

Support a convenient way of specifying a Proxy + credentials.

Retries with async

Thank you for this nice library, with great ergonomics.

I've been playing around with retrying http requests. I found this snippet for retrying any async "work". I soon realised that the httpAsync { ... } async builder fires off the request as soon as it's evaluated, i.e. it's a "hot" task like in C# land, rather than the cold async task idiomatic to F#, so I had to do some additional wrapping, and this is what I eventually came up with FsHttp:

let request =
    async { return! httpAsync { GET "http://httpbin.org/status/500:4,200:1" } }

let! result = retry request (fun r -> int r.statusCode = 200) 10

My question is: am I using this library correctly or am I missing a way to solve this more gracefully? Below is the full runnable example, with a modified retry function which makes the code slightly better (albeit the retry is specialised for http and doesn't work for any async task anymore):

Example.fsx

#r "nuget: SchlenkR.FsHttp, 5.0.0"

open FsHttp
open FsHttp.DslCE

let rec retry request resultOk retries =
    async {
        let! res = request |> Request.sendAsync

        if (resultOk res) || (retries = 0) then
            return res
        else
            return! retry request resultOk (retries - 1)
    }

async {
    let request =
        httpLazy { GET "http://httpbin.org/status/500:4,200:1" }

    let! result = retry request (fun r -> int r.statusCode = 200) 10
    printfn $"status code = {int result.statusCode}"
}
|> Async.RunSynchronously

Use CustomOperation keywords to determin execution mode

Instead of havin http, httpAsync, httpLazy, etc., it could be beneficial to only have one builder that emits different result types (and controls execution) based on keywords. Currently, keywords control formatting (like "exp").

Is it possible to inject an instance of HttpClient?

Since ASP.NET Core introduced IHttpClientFactory and its default DI container supports HttpClient injection out of the box, is it possible to combine it with these computation expressions and inject an instance of the client?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.