An open API service indexing awesome lists of open source software.

https://github.com/vertx-howtos/http-client-howto

Performing HTTP requests to other services
https://github.com/vertx-howtos/http-client-howto

howto http-client vertx

Last synced: 11 months ago
JSON representation

Performing HTTP requests to other services

Awesome Lists containing this project

README

          

= Performing HTTP requests to other services
:page-permalink: /
:page-github: vertx-howtos/http-client-howto

This document will show you how to perform HTTP requests to other services.

== What you will build

You will build a Vert.x application that use the API at https://icanhazdadjoke.com/api and displays a new joke every 3 seconds:

image::run.png[width=600]

The application fits in a single `JokeVerticle` class.

== What you need

* A text editor or IDE
* Java 11 or higher
* Maven or Gradle

== Create a project

The code of this project contains Maven and Gradle build files that are functionally equivalent.

=== Using Maven

Here is the content of the `pom.xml` file you should be using:

[source,xml,role="collapsed"]
.Maven `pom.xml`
----
include::pom.xml[]
----

=== Using Gradle

Assuming you use Gradle with the Kotlin DSL, here is what your `build.gradle.kts` file should look like:

[source,kotlin,role="collapsed"]
.Gradle `build.gradle.kts`
----
include::build.gradle.kts[]
----

== Getting jokes with the Vert.x web client

The Vert.x web client greatly simplifies making HTTP requests compared to the more low-level API found in the Vert.x core API.
It does
The `WebClient` class can be found in the `io.vertx:vertx-web-client` artifact.

To get new jokes, we need to make HTTP GET requests to https://icanhazdadjoke.com/api.
To do that every 3 seconds, we will simply use a Vert.x periodic timer.

The API returns simple JSON objects.
We can test it using a command-line tool like `curl`:

----
$ curl -H "Accept: application/json" https://icanhazdadjoke.com/
{"id":"IJBAsrrzPmb","joke":"What do you call a cow with two legs? Lean beef.","status":200}
----

Here is the code of the `JokeVerticle` class:

[source,java]
----
include::src/main/java/io/vertx/howtos/httpclient/JokeVerticle.java[]
----
<1> Get a `WebClient` attached to the current Vert.x instance.
<2> HTTP GET request for path `/` to host `icanhazdadjoke.com`, on port `443` (HTTPS).
<3> Do not forget to enable SSL encryption.
<4> Explicitly say that we want JSON data.
<5> The response will automatically be converted to JSON.
<6> We expect an HTTP 200 status code, else it will fail the response.
<7> The body is a JSON object, and we write the result to the console.

== Running the application

The `JokeVerticle` already has a `main` method, so it can be used as-is to:

. create a `Vertx` context, then
. deploy `JokeVerticle`.

You can run the application from:

. your IDE, by running the `main` method from the `JokeVerticle` class, or
. with Maven: `mvn compile exec:java`, or
. with Gradle: `./gradlew run` (Linux, macOS) or `gradle run` (Windows).

== Summary

This document covered:

. the Vert.x web client for making HTTP requests,
. extracting data from a JSON response,
. Vert.x periodic tasks.

== See also

- https://vertx.io/docs/vertx-core/java/[The Vert.x core APIs documentation]
- https://vertx.io/docs/vertx-web-client/java/[The Vert.x web client documentation]