how to parse query argument using vertx?
Asked Answered
G

5

7

I wanted to check if we can use getparam to parse start_time and end_time from the below request URL

https://[--hostname--]/sample_app/apthroughput/getAllControllers?start_time=<start time value>&end_time=<end time value>&label=<selected label>
Georgettegeorgi answered 14/10, 2016 at 5:15 Comment(0)
C
7

The parsing of a query string is quite straightforward except for when there are multiple values for the same query param.

e.g.: https://[--hostname--]/sample_app/apthroughput/getAllControllers?type=xxx&type=yyy

in such cases the code below helps getting all the params of a kind in List<String>

This answer gives an idea. Copying from it:

HttpServerRequest request = RoutingContext.request();
MultiMap params =  request.params();
List<String> param = params.getAll("personId");
Here you can get list of personId. URI be like

localhost:8081/myApi?personId=1&personId=2&personId=3
Catabasis answered 6/8, 2018 at 13:58 Comment(0)
C
4

Here's an example:

public static void main(String[] args) {

    Vertx vertx = Vertx.vertx();

    HttpServer server = vertx.createHttpServer();

    server.requestHandler(request -> {

        String startTime = request.getParam("start_time");
        String endTime = request.getParam("end_time");

        // This handler gets called for each request that arrives on the server
        HttpServerResponse response = request.response();
        response.putHeader("content-type", "text/plain");

        // Write to the response and end it
        response.end(String.format("Got start time %s, end time %s", startTime, endTime));
    });

    server.listen(8888);
}

Open http://localhost:8888/?start_time=20161014&end_time=20161015 to see the results.

Crafton answered 15/10, 2016 at 6:44 Comment(6)
I did the same thing but request.getParam("end_time") is returning null. I tried to print request.query() to check all the params passed but its printing only start_time. any idea what can be the reason?Georgettegeorgi
Malformed parameter names, probably. What does your full URL look like now?Crafton
https://[--hostname--]/acp_sample_app/throughput/getAll?start_time=<start_time value>&end_time=<end time value>&label=<selected label>Georgettegeorgi
http://localhost:10210/acp_sample_app/throughput/getAll?start_time=1234567&end_time=12345678&label=123 . Also the path i am using router.route("/sample_app/throughput/getall).handler(...)Georgettegeorgi
Happy to hear. Please mark correct answer, so other Vertx users could reuse it.Crafton
I think the best way to do is as in : #47790606Catabasis
P
1

You can get the parameter String representation but you need to convert the value yourself.

Publish answered 14/10, 2016 at 7:0 Comment(1)
Can you help me with router.route handler?Georgettegeorgi
M
1

I know this is (a bit) late response, but in Vert.X 3.9 to get start_time and end_time parameters you can get them this way:

public void getAllControllersHandler(RoutingContext context) {
   Mutlimap parameters = context.request().params();
   String start_time = parameters.get("start_time");
   String end_time = parameters.get("end_time");
   // now do what you need to with retrieved values :)
}

Also, when your endpoint has to look like this when you are passing it to your Route get() (I presume you are using HTTP Get method):

Router router = Router.router(vertx);
router.get("/sample_app/apthroughput/getAllControllers").handler(this::getAllControllersHandler);

Hope it helps.

P.S. If this solves problem please mark it as accepted answer.

Mufti answered 28/5, 2020 at 9:28 Comment(0)
T
1

@OrkunOzen's answer is good, but the MultiMap uses case-insensitive keys.

For more precise control, you can dig into the code to find this util function called from HttpServerRequest.params() -> io.vertx.core.http.impl.HttpUtils.params(String uri)

This code will give you total control:

QueryStringDecoder queryStringDecoder = new QueryStringDecoder(uri);
Map<String, List<String>> prms = queryStringDecoder.parameters();
Thoughtless answered 31/7, 2021 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.