diff --git a/vlib/x/vweb/tests/vweb_test.v b/vlib/x/vweb/tests/vweb_test.v index 8cb45afb44..a447d672dd 100644 --- a/vlib/x/vweb/tests/vweb_test.v +++ b/vlib/x/vweb/tests/vweb_test.v @@ -249,6 +249,12 @@ fn test_login_with_multipart_form_data_send_by_fetch() { assert x.body == 'username: xmyusernamex | password: xmypassword123x' } +fn test_query_params_are_passed_as_arguments() { + x := http.get('http://${localserver}/query_echo?c=3&a="test"&b=20')! + assert x.status() == .ok + assert x.body == 'a: x"test"x | b: x20x' +} + fn test_host() { mut req := http.Request{ url: 'http://${localserver}/with_host' diff --git a/vlib/x/vweb/tests/vweb_test_server.v b/vlib/x/vweb/tests/vweb_test_server.v index 650370ba36..e08536c1fd 100644 --- a/vlib/x/vweb/tests/vweb_test_server.v +++ b/vlib/x/vweb/tests/vweb_test_server.v @@ -114,6 +114,11 @@ pub fn (mut app ServerApp) file_echo(mut ctx ServerContext) vweb.Result { return ctx.text(ctx.files['file'][0].data) } +@['/query_echo'] +pub fn (mut app ServerApp) query_echo(mut ctx ServerContext, a string, b int) vweb.Result { + return ctx.text('a: x${a}x | b: x${b}x') +} + // Make sure [post] works without the path @[post] pub fn (mut app ServerApp) json(mut ctx ServerContext) vweb.Result { diff --git a/vlib/x/vweb/vweb.v b/vlib/x/vweb/vweb.v index 0094158a9e..355e6a4ad1 100644 --- a/vlib/x/vweb/vweb.v +++ b/vlib/x/vweb/vweb.v @@ -824,6 +824,8 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string // Skip if the host does not match or is empty if route.host == '' || route.host == host { + can_have_data_args := user_context.Context.req.method == .post + || user_context.Context.req.method == .get // Route immediate matches first // For example URL `/register` matches route `/:user`, but `fn register()` // should be called first. @@ -836,12 +838,19 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string } } - if user_context.Context.req.method == .post && method.args.len > 1 { - // Populate method args with form values + if method.args.len > 1 && can_have_data_args { + // Populate method args with form or query values mut args := []string{cap: method.args.len + 1} - for param in method.args[1..] { - args << user_context.Context.form[param.name] + data := if user_context.Context.req.method == .get { + user_context.Context.query + } else { + user_context.Context.form } + + for param in method.args[1..] { + args << data[param.name] + } + app.$method(mut user_context, args) } else { app.$method(mut user_context) @@ -857,7 +866,24 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string } } - app.$method(mut user_context) + if method.args.len > 1 && can_have_data_args { + // Populate method args with form or query values + mut args := []string{cap: method.args.len + 1} + + data := if user_context.Context.req.method == .get { + user_context.Context.query + } else { + user_context.Context.form + } + + for param in method.args[1..] { + args << data[param.name] + } + + app.$method(mut user_context, args) + } else { + app.$method(mut user_context) + } return }