vert-x3 / vertx-web

HTTP web applications for Vert.x
Apache License 2.0
1.11k stars 534 forks source link

need addQueryParams in Web Client #2259

Open dinso opened 2 years ago

dinso commented 2 years ago

Simple feature request

We need addQueryParams in HttpRequestImpl which can add multiple params to the request in one go.

How I came across the need I have a RequestContainer which have all the information related to a request and some common functions which will send requests taking data from RequestContainer. Here I have Map<String,String> params in RequestContainer and I don't think I can dynamically set every params in the list.

I feel this is a problem which many of em will come across.

pendula95 commented 2 years ago

HttpClient from Vert.x Core has a simple API without wrappers for something like this. If you want advance API for making HTTP Request try using vertx/vertx-web-client

This does not mean that what you are trying to do is impossible. You can simply make helper method in your RequestContainer to provide logic similar to this:

        Vertx vertx = Vertx.vertx();
        HttpClient client = vertx.createHttpClient();

        String path = "/test";
        Map<String, String> queryParams = new HashMap<>();
        queryParams.put("param1", "value1");
        queryParams.put("param2", "value2");
        String queryParamsString = queryParams.entrySet().stream().map(entry -> entry.getKey() + "=" + entry.getValue()).collect(Collectors.joining("&"));

        client.request(HttpMethod.GET, 80, "enyivj9hv4vr.x.pipedream.net", path + "?" + queryParamsString, handler -> {
            if (handler.succeeded()) {
                ...
            } else {
                ...
            }
        });