Skip to content

Commit ec59903

Browse files
authored
Fixed encoding of spaces (#24)
* Fixed encoding of spaces Spaces must be replaced by `+` instead of `%20`. 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] <...>
1 parent e31be3c commit ec59903

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ New features:
1010

1111
Bugfixes:
1212

13+
- Fixed encoding of spaces. Spaces must be replaced by `+` instead of `%20` (#24 by @dederer)
14+
1315
Other improvements:
1416

1517
## [v6.0.0](https://github.com/purescript-contrib/purescript-form-urlencoded/releases/tag/v6.0.0) - 2021-02-26

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Use the `encode` function to properly encode an array of key-value pairs, and th
2828
> [ Tuple "hey" Nothing
2929
> , Tuple "Oh" (Just "Let's go!")
3030
> ]
31-
Just "hey&Oh=Let's%20go!"
31+
Just "hey&Oh=Let's+go!"
3232
3333
> decode "a=aa&b=bb"
3434
Just (FormURLEncoded [(Tuple "a" (Just "aa")),(Tuple "b" (Just "bb"))])

src/Data/FormURLEncoded.purs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Data.String (joinWith, split) as String
88
import Data.String.Pattern (Pattern(..))
99
import Data.Traversable (traverse)
1010
import Data.Tuple (Tuple(..))
11-
import JSURI (decodeURIComponent, encodeURIComponent)
11+
import JSURI (decodeFormURLComponent, encodeFormURLComponent)
1212

1313
-- | `FormURLEncoded` is an ordered list of key-value pairs with possible duplicates.
1414
newtype FormURLEncoded = FormURLEncoded (Array (Tuple String (Maybe String)))
@@ -35,14 +35,14 @@ encode :: FormURLEncoded -> Maybe String
3535
encode = map (String.joinWith "&") <<< traverse encodePart <<< toArray
3636
where
3737
encodePart = case _ of
38-
Tuple k Nothing -> encodeURIComponent k
39-
Tuple k (Just v) -> (\key val -> key <> "=" <> val) <$> encodeURIComponent k <*> encodeURIComponent v
38+
Tuple k Nothing -> encodeFormURLComponent k
39+
Tuple k (Just v) -> (\key val -> key <> "=" <> val) <$> encodeFormURLComponent k <*> encodeFormURLComponent v
4040

4141
-- | Decode `FormURLEncoded` from `application/x-www-form-urlencoded`.
4242
decode :: String -> Maybe FormURLEncoded
4343
decode = map FormURLEncoded <<< traverse decodePart <<< String.split (Pattern "&")
4444
where
4545
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
46+
[k, v] -> (\key val -> Tuple key $ Just val) <$> decodeFormURLComponent k <*> decodeFormURLComponent v
47+
[k] -> Tuple <$> decodeFormURLComponent k <*> pure Nothing
4848
_ -> Nothing

test/Main.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ main = do
2727

2828
testDecode "a=b&%8A=c" Nothing
2929

30+
testDecode "a+b=aa+bb" (Just $ FormURLEncoded [ Tuple "a b" $ Just "aa bb" ])
31+
3032
testEncode (FormURLEncoded [ Tuple "a" $ Just "aa", Tuple "b" $ Just "bb" ]) $ Just "a=aa&b=bb"
3133

3234
testEncode (FormURLEncoded [ Tuple "this" $ Just "this=this" ]) $ Just "this=this%3Dthis"
@@ -41,6 +43,8 @@ main = do
4143
])
4244
(Just "&x=x&&y=y&z=")
4345

46+
testEncode (FormURLEncoded [ Tuple "a b" $ Just "aa bb" ]) $ Just "a+b=aa+bb"
47+
4448
where
4549

4650
testDecode :: String -> Maybe FormURLEncoded -> Effect Unit

0 commit comments

Comments
 (0)