Search code examples
f#suave

how to set up Suave to access a post message on any route, and binding to "*"


I need to set up a service that need to accept POST messages to ANY route on that IP and have the web server listening to ALL IPs the machine has

I can make a list of local IPs easily, add 127.0.0.1 to it for testing.

How can I set up a callback on any post request, with the contents? I've just started to look at it 1h ago so the answer may be obvious.


Solution

  • This will handle all POST requests sent to whatever bindings you'd like:

    open Suave
    open Suave.Filters
    open Suave.Operators
    open Suave.Successful
    
    [<EntryPoint>]
    let main argv =
    
            // list your bindings here
        let bindings =
            [
                "127.0.0.1", 8080
                "127.0.0.1", 8081
            ] |> List.map (fun (addr, port) ->
                HttpBinding.createSimple HTTP addr port)
        let cfg =
            { defaultConfig with bindings = bindings }
    
            // handle all POST requests
        let app =
            POST >=> request (fun req ->
                OK $"POST received: {req.path}")
    
        startWebServer cfg app
    
        0
    

    Powershell test: Invoke-WebRequest -uri "http://127.0.0.1:8081/Hello" -Method POST

    Output: POST received: /Hello