Skip to content

Spaces must be encoded as "+" #23

Closed
@dederer

Description

@dederer

The encode function encodes a space as %20, not + as required.

> encode $ fromArray 
>   [ Tuple "hey" Nothing
>   , Tuple "Oh" (Just "Let's go!")
>   ]
Just "hey&Oh=Let's%20go!"

Expected result:

Just "hey&Oh=Let's+go!"

W3C

https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1

application/x-www-form-urlencoded Forms submitted with this content type must be encoded as follows: <...> Space characters are replaced by `+', and then reserved characters are escaped as described in [RFC1738] <...>

FormURLEncoded.purs

https://github.com/purescript-contrib/purescript-form-urlencoded/blob/v6.0.1/src/Data/FormURLEncoded.purs

Instead of functions encodeURIComponent and decodeURIComponent, should use functions encodeFormURLComponent and decodeFormURLComponent.

33: -- | Encode `FormURLEncoded` as `application/x-www-form-urlencoded`.
34: encode :: FormURLEncoded -> Maybe String
35: encode = map (String.joinWith "&") <<< traverse encodePart <<< toArray
36:   where
37:     encodePart = case _ of
38:       Tuple k Nothing -> encodeURIComponent k
                             ^^^^^^^^^^^^^^^^^^
39:       Tuple k (Just v) -> (\key val -> key <> "=" <> val) <$> encodeURIComponent k <*> encodeURIComponent v
                                                                  ^^^^^^^^^^^^^^^^^^       ^^^^^^^^^^^^^^^^^^
40:
41: -- | Decode `FormURLEncoded` from `application/x-www-form-urlencoded`.
42: decode :: String -> Maybe FormURLEncoded
43: decode = map FormURLEncoded <<< traverse decodePart <<< String.split (Pattern "&")
44:   where
45:     decodePart = String.split (Pattern "=") >>> case _ of
46:       [k, v] -> (\key val -> Tuple key $ Just val) <$> decodeURIComponent k <*> decodeURIComponent v
                                                           ^^^^^^^^^^^^^^^^^^       ^^^^^^^^^^^^^^^^^^
47:       [k]    -> Tuple <$> decodeURIComponent k <*> pure Nothing
                              ^^^^^^^^^^^^^^^^^^

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething that should function correctly isn't.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions