简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM