Code Monkey home page Code Monkey logo

Comments (3)

sordina avatar sordina commented on May 12, 2024

It appears that this is supported but MethodOverride only handles query parameters, however, while testing it seems that mkEnv doesn't handle encoded parameters from other StdMethod types than POST:

mkEnv :: StdMethod -> Request -> [Param] -> ResourceT IO ActionEnv
mkEnv method req captures = do
    b <- BL.fromChunks <$> lazyConsume (requestBody req)

    let parameters = captures ++ formparams ++ queryparams
        formparams = case (method, lookup "Content-Type" [(CI.mk k, CI.mk v) | (k,v) <- requestHeaders req]) of
                        (POST, Just "application/x-www-form-urlencoded") -> parseEncodedParams $ mconcat $ BL.toChunks b
                        _ -> []
        queryparams = parseEncodedParams $ rawQueryString req

    return $ Env req parameters b

I'm not sure of the best way to generalize this, but people will probably want to be able to send parameters through the usual means. As a test I used a wildcard to capture all form-encoded parameters in mkEnv and this appears to work:

    let parameters = captures ++ formparams ++ queryparams
        formparams = case (method, lookup "Content-Type" [(CI.mk k, CI.mk v) | (k,v) <- requestHeaders req]) of
                        (_, Just "application/x-www-form-urlencoded") -> parseEncodedParams $ mconcat $ BL.toChunks b
                        _ -> []

I have not tested if this breaks existing functionality at this point.

from scotty.

xich avatar xich commented on May 12, 2024

Yes, MethodOverride only works with query parameters, so your html would need to be:

<form method="POST" action="/update?_method=PUT">
    <input type="text" name="nickname" />
    <input type="submit" />
</form>

Unfortunately, this is someone else's package, but I'm sure they would accept a patch if you found a clean way to look for "_method" in the form parameters.

As for mkEnv: yes this is very incomplete. There are several ways to encode form data, especially once you get to files and multipart data. I've only implemented the most basic method here because that is all I've needed so far. ;-)

Ideally, I'd rather form data get parsed in middleware, and attached to the request object on the way in. The WAI Request object has a Vault, which is probably where we'd store this. Then mkEnv could just look there.

I might take a crack at this later tonight, depending on how much else I get done today. :-P

from scotty.

sordina avatar sordina commented on May 12, 2024

Thanks Andrew.

I agree that this would be best handled with middleware. It should probably be included in wai-extra, then as you say, mkEnv can just pull the values out of the vault. I've begun adding a middleware to wai-extra that will parse application/x-www-form-urlencoded data similar to how Scotty does, however, I'm not experienced with the other encodings you mentioned.

What should I be looking out for? Are there any existing libraries I can use that you know of?

from scotty.

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.