Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to parse query argument using vertx?

Tags:

java

rest

vert.x

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>
like image 303
nocturnal Avatar asked Oct 14 '16 05:10

nocturnal


2 Answers

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
like image 173
Orkun Ozen Avatar answered Oct 16 '22 13:10

Orkun Ozen


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.

like image 3
Alexey Soshin Avatar answered Oct 16 '22 14:10

Alexey Soshin