{"id":20796733,"url":"https://github.com/vertx-howtos/http-client-howto","last_synced_at":"2025-05-06T07:57:46.782Z","repository":{"id":101018222,"uuid":"169255052","full_name":"vertx-howtos/http-client-howto","owner":"vertx-howtos","description":"Performing HTTP requests to other services","archived":false,"fork":false,"pushed_at":"2024-12-03T15:43:33.000Z","size":758,"stargazers_count":3,"open_issues_count":0,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-31T01:23:05.123Z","etag":null,"topics":["howto","http-client","vertx"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vertx-howtos.png","metadata":{"files":{"readme":"README.adoc","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-05T14:35:15.000Z","updated_at":"2024-12-04T17:03:57.000Z","dependencies_parsed_at":null,"dependency_job_id":"e77cb77d-fd0c-444f-9998-3ffd386260f8","html_url":"https://github.com/vertx-howtos/http-client-howto","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vertx-howtos%2Fhttp-client-howto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vertx-howtos%2Fhttp-client-howto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vertx-howtos%2Fhttp-client-howto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vertx-howtos%2Fhttp-client-howto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vertx-howtos","download_url":"https://codeload.github.com/vertx-howtos/http-client-howto/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252645485,"owners_count":21781967,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["howto","http-client","vertx"],"created_at":"2024-11-17T16:28:50.728Z","updated_at":"2025-05-06T07:57:46.767Z","avatar_url":"https://github.com/vertx-howtos.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"= Performing HTTP requests to other services\n:page-permalink: /\n:page-github: vertx-howtos/http-client-howto\n\nThis document will show you how to perform HTTP requests to other services.\n\n== What you will build\n\nYou will build a Vert.x application that use the API at https://icanhazdadjoke.com/api and displays a new joke every 3 seconds:\n\nimage::run.png[width=600]\n\nThe application fits in a single `JokeVerticle` class.\n\n== What you need\n\n* A text editor or IDE\n* Java 11 or higher\n* Maven or Gradle\n\n== Create a project\n\nThe code of this project contains Maven and Gradle build files that are functionally equivalent.\n\n=== Using Maven\n\nHere is the content of the `pom.xml` file you should be using:\n\n[source,xml,role=\"collapsed\"]\n.Maven `pom.xml`\n----\ninclude::pom.xml[]\n----\n\n=== Using Gradle\n\nAssuming you use Gradle with the Kotlin DSL, here is what your `build.gradle.kts` file should look like:\n\n[source,kotlin,role=\"collapsed\"]\n.Gradle `build.gradle.kts`\n----\ninclude::build.gradle.kts[]\n----\n\n== Getting jokes with the Vert.x web client\n\nThe Vert.x web client greatly simplifies making HTTP requests compared to the more low-level API found in the Vert.x core API.\nIt does\nThe `WebClient` class can be found in the `io.vertx:vertx-web-client` artifact.\n\nTo get new jokes, we need to make HTTP GET requests to https://icanhazdadjoke.com/api.\nTo do that every 3 seconds, we will simply use a Vert.x periodic timer.\n\nThe API returns simple JSON objects.\nWe can test it using a command-line tool like `curl`:\n\n----\n$ curl -H \"Accept: application/json\" https://icanhazdadjoke.com/\n{\"id\":\"IJBAsrrzPmb\",\"joke\":\"What do you call a cow with two legs? Lean beef.\",\"status\":200}\n----\n\nHere is the code of the `JokeVerticle` class:\n\n[source,java]\n----\ninclude::src/main/java/io/vertx/howtos/httpclient/JokeVerticle.java[]\n----\n\u003c1\u003e Get a `WebClient` attached to the current Vert.x instance.\n\u003c2\u003e HTTP GET request for path `/` to host `icanhazdadjoke.com`, on port `443` (HTTPS).\n\u003c3\u003e Do not forget to enable SSL encryption.\n\u003c4\u003e Explicitly say that we want JSON data.\n\u003c5\u003e The response will automatically be converted to JSON.\n\u003c6\u003e We expect an HTTP 200 status code, else it will fail the response.\n\u003c7\u003e The body is a JSON object, and we write the result to the console.\n\n== Running the application\n\nThe `JokeVerticle` already has a `main` method, so it can be used as-is to:\n\n. create a `Vertx` context, then\n. deploy `JokeVerticle`.\n\nYou can run the application from:\n\n. your IDE, by running the `main` method from the `JokeVerticle` class, or\n. with Maven: `mvn compile exec:java`, or\n. with Gradle: `./gradlew run` (Linux, macOS) or `gradle run` (Windows).\n\n== Summary\n\nThis document covered:\n\n. the Vert.x web client for making HTTP requests,\n. extracting data from a JSON response,\n. Vert.x periodic tasks.\n\n== See also\n\n- https://vertx.io/docs/vertx-core/java/[The Vert.x core APIs documentation]\n- https://vertx.io/docs/vertx-web-client/java/[The Vert.x web client documentation]\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvertx-howtos%2Fhttp-client-howto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvertx-howtos%2Fhttp-client-howto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvertx-howtos%2Fhttp-client-howto/lists"}