Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #79 from marianovalles/master

Decode URI params when using elli_request:get_arg/3
  • Loading branch information...
commit 49cd96ea617365bd3e9caa966839da78cfc7be56 2 parents 4627737 + 3f8830f
@knutin authored
View
5 src/elli_example_callback.erl
@@ -46,6 +46,11 @@ handle('GET', [<<"hello">>, <<"iolist">>], Req) ->
Name = elli_request:get_arg(<<"name">>, Req),
{ok, [], [<<"Hello ">>, Name]};
+handle('GET', [<<"decoded-hello">>], Req) ->
+ %% Fetch a URI decoded GET argument from the URL.
+ Name = elli_request:get_arg_decoded(<<"name">>, Req, <<"undefined">>),
+ {ok, [], <<"Hello ", Name/binary>>};
+
handle('GET', [<<"type">>], Req) ->
Name = elli_request:get_arg(<<"name">>, Req),
%% Fetch a header.
View
9 src/elli_request.erl
@@ -10,6 +10,8 @@
, raw_path/1
, query_str/1
, get_header/2
+ , get_arg_decoded/2
+ , get_arg_decoded/3
, get_arg/2
, get_arg/3
, get_args/1
@@ -64,6 +66,13 @@ get_arg(Key, #req{} = Req) ->
get_arg(Key, #req{args = Args}, Default) ->
proplists:get_value(Key, Args, Default).
+get_arg_decoded(Key, #req{} = Req) ->
+ get_arg_decoded(Key, Req, undefined).
+
+get_arg_decoded(Key, #req{args = Args}, Default) ->
+ EncodedValue = proplists:get_value(Key, Args, Default),
+ list_to_binary(http_uri:decode(binary_to_list(EncodedValue))).
+
%% @doc Parses application/x-www-form-urlencoded body into a proplist
body_qs(#req{body = Body} = Req) ->
case get_header(<<"Content-Type">>, Req) of
View
5 test/elli_tests.erl
@@ -19,6 +19,7 @@ elli_test_() ->
?_test(accept_content_type()),
?_test(user_connection()),
?_test(get_args()),
+ ?_test(decoded_get_args()),
?_test(post_args()),
?_test(shorthand()),
?_test(too_many_headers()),
@@ -124,6 +125,10 @@ get_args() ->
{ok, Response} = httpc:request("http://localhost:3001/hello?name=knut"),
?assertEqual("Hello knut", body(Response)).
+decoded_get_args() ->
+ {ok, Response} = httpc:request("http://localhost:3001/decoded-hello?name=knut%3D"),
+ ?assertEqual("Hello knut=", body(Response)).
+
post_args() ->
Body = <<"name=foo&baz=quux">>,
ContentType = "application/x-www-form-urlencoded",
Please sign in to comment.
Something went wrong with that request. Please try again.