Skip to content

Commit

Permalink
URI template with Web Client example
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Nov 15, 2024
1 parent bea1370 commit 843d13f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
14 changes: 14 additions & 0 deletions web-client-examples/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ The server logs the query string on the console.
The client add several query parameters to the http request with the fluent API, the client takes care of
encoding the parameters.

== URI Templates

This example uses the web client to send a request with query params using the fluent API and log the response.

This example is pretty much similar to the query params example, however it leverages Vert.x URI Templates
to encode the query parameters in the request URI.

First you need to run the server then you can run the client.

link:src/main/java/io/vertx/example/webclient/uritemplate/Server.java[Java HTTP server]
link:src/main/java/io/vertx/example/webclient/uritemplate/Client.java[Java URI Template HTTP client]

The server logs the query string on the console.

== HTTPS

This example is like the simple example but shows how to configure the client for sending SSL/TLS requests.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.vertx.example.webclient.uritemplate;

import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.ext.web.client.WebClient;
import io.vertx.launcher.application.VertxApplication;
import io.vertx.uritemplate.UriTemplate;

/*
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
public class Client extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{Client.class.getName()});
}

private static final UriTemplate REQUEST_URI = UriTemplate.of("/{?firstName}{&lastName}{&male}");

private WebClient client;

@Override
public Future<?> start() throws Exception {

client = WebClient.create(vertx);

return client.get(8080, "localhost", REQUEST_URI)
.setTemplateParam("firstName", "Dale")
.setTemplateParam("lastName", "Cooper")
.setTemplateParam("male", "true")
.send()
.onSuccess(response -> System.out.println("Got HTTP response with status " + response.statusCode()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.vertx.example.webclient.uritemplate;

import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.launcher.application.VertxApplication;

/*
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
public class Server extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{Server.class.getName()});
System.out.println("Server started");
}

@Override
public Future<?> start() throws Exception {

return vertx.createHttpServer().requestHandler(req -> {
System.out.println("Got request with query params: " + req.query());
req.response().end();
}).listen(8080);
}
}

0 comments on commit 843d13f

Please sign in to comment.