Zaid-Ajaj / Fable.Remoting

Type-safe communication layer (RPC-style) for F# featuring Fable and .NET Apps
https://zaid-ajaj.github.io/Fable.Remoting/
MIT License
272 stars 54 forks source link

Q: GET with multiple parameters #368

Closed KingKnecht closed 1 month ago

KingKnecht commented 1 month ago

I want to implement an API for GET with two parameters.

to be able to do something like this:

http://localhost:5000/authorize?username=john&password=12345

I have tried:

type IRestApi =  { 
          authorize:  string -> string -> Async<Option<AuthToken>> 
         }

and with a single parameter like this:

type username = string
type password = string
type authParameters = {
        username : username
        password : password
    }

type IRestApi =  {
          authorize:  authParameters -> Async<Option<AuthToken>>
      }

my Fable webApi function looks like this:

let webApi =
    Remoting.createApi ()
    |> Remoting.fromContext (fun (ctx: HttpContext) -> ctx.GetService<ServerApi>().Build())
    |> Remoting.withRouteBuilder routerPaths
    |> Remoting.buildHttpHandler

Implementation of ServerApi looks like this:

type ServerApi(logger: ILogger<ServerApi>, config: IConfiguration) =
    member this.Authorization (authparams: authParameters) =
        async {
            // Your super secure no-password-over-cleartext solution here
            if authparams.username = "" || authparams.password <> "aaa" then
                return None
            else
                do Console.WriteLine("Bingo")
                return Some(AuthToken "auth-0001") 
        }

    member this.Build() : IRestApi = { authorize = this.Authorization }

But my implementation of Authorization gets never called. How could I achieve that?

Zaid-Ajaj commented 1 month ago

I want to implement an API for GET with two parameters.

Any remoting function that takes arguments maps to POST requests, only functions that take unit are mapped to GET in HTTP. Read more about it here https://zaid-ajaj.github.io/Fable.Remoting/#/advanced/raw-http-communication

You cannot implement GET request like you mentioned before from remoting. However, you can implement the GET route as part of your web application, outside of the remoting API and let it call the same function via: ctx.GetService<ServerApi>().authorize ...

KingKnecht commented 1 month ago

Got it running. Thanks!