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>
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>
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
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.
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 You can get the parameter String representation but you need to convert the value yourself.
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.
@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();
© 2022 - 2024 — McMap. All rights reserved.