GithubHelp home page GithubHelp logo

Comments (7)

dustinmoris avatar dustinmoris commented on July 23, 2024

Hey, I will get back to you with an example over the weekend and include it into the current sample app! That is a good question, thanks for asking!

from giraffe.

nojaf avatar nojaf commented on July 23, 2024

Modelbinding appears to be to tightly coupled to Mvc, aspnet/Mvc#6109.
Parsing the raw request to string (json) and converting the json to the model seems like an option.

Question is do we want other ways of extracting the model from the request? Would it make sense to have our own modelbinding?

from giraffe.

nojaf avatar nojaf commented on July 23, 2024

I'm able to get the json from the request

let readAllBytes (s : Stream) = 
    let ms = new MemoryStream()
    s.CopyTo(ms)
    ms.ToArray()

// ...

        POST >=> 
            choose [
                route "/contact" >=> (fun (ctx: HttpHandlerContext) -> 
                    async {
                        let model = 
                            ctx.HttpContext.Request.Body
                            |> readAllBytes
                            |> Encoding.UTF8.GetString
                            |> JsonConvert.DeserializeObject
                        
                        sprintf "Received model %s" (model.ToString())
                        |> Console.WriteLine

                        let json = JsonConvert.SerializeObject(model)
                        return! ctx |> (sprintf "Received %s" json |> text)
                    }                    
                )
            ]

Might be a good idea to provide some things out of the box for this.

from giraffe.

dustinmoris avatar dustinmoris commented on July 23, 2024

Agreed. It would be nice if there would be a function similar like let model = bindModel<Person> ctx.

I would expect that this function would also inspect the requests' Content-Type and at least support the deserialization of a JSON, XML or URL encoded object by default with a facility for users to extend if they have some custom binding requriements.

from giraffe.

nojaf avatar nojaf commented on July 23, 2024

Perhaps the return type of bindModel should be Result<'TOk, 'TError>.
See https://gist.github.com/nojaf/cf176f304a7843bbf15e6a3b3d9dc9f2

from giraffe.

dustinmoris avatar dustinmoris commented on July 23, 2024

Just published Giraffe.ModelBinding this morning.

Have a look if the updated documentation makes sense and if you can think of any improvements?

I think an additional bindQueryString function would be useful which can bind a model from query string parameters, but I didn't have time to add this one as well now.

from giraffe.

nojaf avatar nojaf commented on July 23, 2024

Great work, simple and clean. This is a good starting point and I would create separate issues for missing functionality. F.ex. complex type properties with the form data or a combination of form data and querystring, etc.

from giraffe.

Related Issues (20)

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.