= Vert.x Web Client
:toc: left
:lang: groovy
:groovy: groovy

Vert.x Web Client is an asynchronous HTTP and HTTP/2 client.

The Web Client makes easy to do HTTP request/response interactions with a web server, and provides advanced
features like:

* Json body encoding / decoding
* request/response pumping
* request parameters
* unified error handling
* form submissions

The web client does not deprecate the Vert.x Core `link:../../apidocs/io/vertx/core/http/HttpClient.html[HttpClient]`, indeed it is based on
this client and inherits its configuration and great features like pooling, HTTP/2 support, pipelining support, etc...
The `link:../../apidocs/io/vertx/core/http/HttpClient.html[HttpClient]` should be used when fine grained control over the HTTP
requests/responses is necessary.

The web client does not provide a WebSocket API, the Vert.x Core `link:../../apidocs/io/vertx/core/http/HttpClient.html[HttpClient]` should
be used.

== Using the web client

To use Vert.x Web Client, add the following dependency to the _dependencies_ section of your build descriptor:

* Maven (in your `pom.xml`):

[source,xml,subs="+attributes"]
----
<dependency>
  <groupId>io.vertx</groupId>
  <artifactId>vertx-web-client</artifactId>
  <version>3.4.1</version>
</dependency>
----

* Gradle (in your `build.gradle` file):

[source,groovy,subs="+attributes"]
----
dependencies {
  compile 'io.vertx:vertx-web-client:3.4.1'
}
----

== Re-cap on Vert.x core HTTP client

Vert.x Web Client uses the API from Vert.x core, so it's well worth getting familiar with the basic concepts of using
`link:../../apidocs/io/vertx/core/http/HttpClient.html[HttpClient]` using Vert.x core, if you're not already.

== Creating a web client

You create an `link:../../apidocs/io/vertx/ext/web/client/WebClient.html[WebClient]` instance with default options as follows

[source,groovy]
----
def client = WebClient.create(vertx)

----

If you want to configure options for the client, you create it as follows

[source,groovy]
----
def options = [
  userAgent:"My-App/1.2.3"
]
options.keepAlive = false
def client = WebClient.create(vertx, options)

----

Web Client options inherit Http Client options so you can set any one of them.

If your already have an HTTP Client in your application you can also reuse it

[source,groovy]
----
def client = WebClient.wrap(httpClient)

----

== Making requests

=== Simple requests with no body

Often, you’ll want to make HTTP requests with no request body. This is usually the case with HTTP GET, OPTIONS
and HEAD requests

[source,groovy]
----

def client = WebClient.create(vertx)

// Send a GET request
client.get(8080, "myserver.mycompany.com", "/some-uri").send({ ar ->
  if (ar.succeeded()) {
    // Obtain response
    def response = ar.result()

    println("Received response with status code${response.statusCode()}")
  } else {
    println("Something went wrong ${ar.cause().getMessage()}")
  }
})

// Send a HEAD request
client.head(8080, "myserver.mycompany.com", "/some-uri").send({ ar ->
  if (ar.succeeded()) {
    // Obtain response
    def response = ar.result()

    println("Received response with status code${response.statusCode()}")
  } else {
    println("Something went wrong ${ar.cause().getMessage()}")
  }
})

----

You can add query parameters to the request URI in a fluent fashion

[source,groovy]
----
Code not translatable
----

Any request URI parameter will pre-populate the request

[source,groovy]
----
def request = client.get(8080, "myserver.mycompany.com", "/some-uri?param1=param1_value&param2=param2_value")

// Add param3
request.addQueryParam("param3", "param3_value")

// Overwrite param2
request.setQueryParam("param2", "another_param2_value")

----

Setting a request URI discards existing query parameters

[source,groovy]
----
def request = client.get(8080, "myserver.mycompany.com", "/some-uri")

// Add param1
request.addQueryParam("param1", "param1_value")

// Overwrite param1 and add param2
request.uri("/some-uri?param1=param1_value&param2=param2_value")

----

=== Writing request bodies

When you need to make a request with a body, you use the same API and call then `sendXXX` methods
that expects a body to send.

Use `link:../../apidocs/io/vertx/ext/web/client/HttpRequest.html#sendBuffer-io.vertx.core.buffer.Buffer-io.vertx.core.Handler-[sendBuffer]` to send a buffer body

[source,groovy]
----
// Send a buffer to the server using POST, the content-length header will be set for you
client.post(8080, "myserver.mycompany.com", "/some-uri").sendBuffer(buffer, { ar ->
  if (ar.succeeded()) {
    // Ok
  }
})

----

Sending a single buffer is useful but often you don't want to load fully the content in memory because
it may be too large or you want to handle many concurrent requests and want to use just the minimum
for each request. For this purpose the web client can send `ReadStream<Buffer>` (e.g a
`link:../../apidocs/io/vertx/core/file/AsyncFile.html[AsyncFile]` is a ReadStream<Buffer>`) with the `link:../../apidocs/io/vertx/ext/web/client/HttpRequest.html#sendStream-io.vertx.core.streams.ReadStream-io.vertx.core.Handler-[sendStream]` method

[source,groovy]
----
Code not translatable
----

The web client takes care of setting up the transfer pump for you. Since the length of the stream is not know
the request will use chunked transfer encoding .

When you know the size of the stream, you shall specify before using the `content-length` header

[source,groovy]
----
fs.open("content.txt", [:], { fileRes ->
  if (fileRes.succeeded()) {
    def fileStream = fileRes.result()

    def fileLen = "1024"

    // Send the file to the server using POST
    client.post(8080, "myserver.mycompany.com", "/some-uri").putHeader("content-length", fileLen).sendStream(fileStream, { ar ->
      if (ar.succeeded()) {
        // Ok
      }
    })
  }
})

----

The POST will not be chunked.

==== Json bodies

Often you’ll want to send Json body requests, to send a `link:../../apidocs/io/vertx/core/json/JsonObject.html[JsonObject]`
use the `link:../../apidocs/io/vertx/ext/web/client/HttpRequest.html#sendJsonObject-io.vertx.core.json.JsonObject-io.vertx.core.Handler-[sendJsonObject]`

[source,groovy]
----
client.post(8080, "myserver.mycompany.com", "/some-uri").sendJsonObject([
  firstName:"Dale",
  lastName:"Cooper"
], { ar ->
  if (ar.succeeded()) {
    // Ok
  }
})

----

In Java, Groovy or Kotlin, you can use the `link:../../apidocs/io/vertx/ext/web/client/HttpRequest.html#sendJson-java.lang.Object-io.vertx.core.Handler-[sendJson]` method that maps
a POJO (Plain Old Java Object) to a Json object using `link:../../apidocs/io/vertx/core/json/Json.html#encode-java.lang.Object-[Json.encode]`
method

[source,groovy]
----
client.post(8080, "myserver.mycompany.com", "/some-uri").sendJson(new examples.WebClientExamples.User("Dale", "Cooper"), { ar ->
  if (ar.succeeded()) {
    // Ok
  }
})

----

NOTE: the `link:../../apidocs/io/vertx/core/json/Json.html#encode-java.lang.Object-[Json.encode]` uses the Jackson mapper to encode the object
to Json.

==== Form submissions

You can send http form submissions bodies with the `link:../../apidocs/io/vertx/ext/web/client/HttpRequest.html#sendForm-io.vertx.core.MultiMap-io.vertx.core.Handler-[sendForm]`
variant.

[source,groovy]
----
def form = MultiMap.caseInsensitiveMultiMap()
form.set("firstName", "Dale")
form.set("lastName", "Cooper")

// Submit the form as a form URL encoded body
client.post(8080, "myserver.mycompany.com", "/some-uri").sendForm(form, { ar ->
  if (ar.succeeded()) {
    // Ok
  }
})

----

By default the form is submitted with the `application/x-www-form-urlencoded` content type header. You can set
the `content-type` header to `multipart/form-data` instead

[source,groovy]
----
def form = MultiMap.caseInsensitiveMultiMap()
form.set("firstName", "Dale")
form.set("lastName", "Cooper")

// Submit the form as a multipart form body
client.post(8080, "myserver.mycompany.com", "/some-uri").putHeader("content-type", "multipart/form-data").sendForm(form, { ar ->
  if (ar.succeeded()) {
    // Ok
  }
})

----

NOTE: at the moment multipart files are not supported, it will likely be supported in a later revision
of the API.

=== Writing request headers

You can write headers to a request using the headers multi-map as follows:

[source,groovy]
----
def request = client.get(8080, "myserver.mycompany.com", "/some-uri")
def headers = request.headers()
headers.set("content-type", "application/json")
headers.set("other-header", "foo")

----

The headers are an instance of `link:../../apidocs/io/vertx/core/MultiMap.html[MultiMap]` which provides operations for adding,
setting and removing entries. Http headers allow more than one value for a specific key.

You can also write headers using putHeader

[source,groovy]
----
def request = client.get(8080, "myserver.mycompany.com", "/some-uri")
request.putHeader("content-type", "application/json")
request.putHeader("other-header", "foo")

----

=== Reusing requests

The `link:../../apidocs/io/vertx/ext/web/client/HttpRequest.html#send-io.vertx.core.Handler-[send]` method can be called multiple times
safely, making it very easy to configure and reuse `link:../../apidocs/io/vertx/ext/web/client/HttpRequest.html[HttpRequest]` objects

[source,groovy]
----
def get = client.get(8080, "myserver.mycompany.com", "/some-uri")
get.send({ ar ->
  if (ar.succeeded()) {
    // Ok
  }
})

// Same request again
get.send({ ar ->
  if (ar.succeeded()) {
    // Ok
  }
})

----

When you need to mutate a request, the `link:../../apidocs/io/vertx/ext/web/client/HttpRequest.html#copy--[copy]` returns a copy of the
request

[source,groovy]
----
def get = client.get(8080, "myserver.mycompany.com", "/some-uri")
get.send({ ar ->
  if (ar.succeeded()) {
    // Ok
  }
})

// Same request again
get.putHeader("an-header", "with-some-value").send({ ar ->
  if (ar.succeeded()) {
    // Ok
  }
})

----

=== Timeouts

You can set a timeout for a specific http request using `link:../../apidocs/io/vertx/ext/web/client/HttpRequest.html#timeout-long-[timeout]`.

[source,groovy]
----
client.get(8080, "myserver.mycompany.com", "/some-uri").timeout(5000).send({ ar ->
  if (ar.succeeded()) {
    // Ok
  } else {
    // Might be a timeout when cause is java.util.concurrent.TimeoutException
  }
})

----

If the request does not return any data within the timeout period an exception will be passed to the response
handler.

== Handling http responses

When the web client sends a request you always deal with a single async result `link:../../apidocs/io/vertx/ext/web/client/HttpResponse.html[HttpResponse]`.

On a success result the callback happens after the response has been received

[source,groovy]
----
client.get(8080, "myserver.mycompany.com", "/some-uri").send({ ar ->
  if (ar.succeeded()) {

    def response = ar.result()

    println("Received response with status code${response.statusCode()}")
  } else {
    println("Something went wrong ${ar.cause().getMessage()}")
  }
})

----

WARNING: responses are fully buffered, use `link:../../apidocs/io/vertx/ext/web/codec/BodyCodec.html#pipe-io.vertx.core.streams.WriteStream-[BodyCodec.pipe]`
to pipe the response to a write stream

=== Decoding responses

By default the web client provides an http response body as a `Buffer` and does not apply
any decoding.

Custom response body decoding can be achieved using `link:../../apidocs/io/vertx/ext/web/codec/BodyCodec.html[BodyCodec]`:

* Plain String
* Json object
* Json mapped POJO
* `link:../../apidocs/io/vertx/core/streams/WriteStream.html[WriteStream]`

A body codec can decode an arbitrary binary data stream into a specific object instance, saving you the decoding
step in your response handlers.

Use `link:../../apidocs/io/vertx/ext/web/codec/BodyCodec.html#jsonObject--[BodyCodec.jsonObject]` To decode a Json object:

[source,groovy]
----
client.get(8080, "myserver.mycompany.com", "/some-uri").as(BodyCodec.jsonObject()).send({ ar ->
  if (ar.succeeded()) {
    def response = ar.result()

    def body = response.body()

    println("Received response with status code${response.statusCode()} with body ${body}")
  } else {
    println("Something went wrong ${ar.cause().getMessage()}")
  }
})

----

In Java, Groovy or Kotlin, custom Json mapped POJO can be decoded

[source,groovy]
----
client.get(8080, "myserver.mycompany.com", "/some-uri").as(BodyCodec.json(examples.WebClientExamples.User.class)).send({ ar ->
  if (ar.succeeded()) {
    def response = ar.result()

    def user = response.body()

    println("Received response with status code${response.statusCode()} with body ${user.getFirstName()} ${user.getLastName()}")
  } else {
    println("Something went wrong ${ar.cause().getMessage()}")
  }
})

----

When large response are expected, use the `link:../../apidocs/io/vertx/ext/web/codec/BodyCodec.html#pipe-io.vertx.core.streams.WriteStream-[BodyCodec.pipe]`.
This body codec pumps the response body buffers to a `link:../../apidocs/io/vertx/core/streams/WriteStream.html[WriteStream]`
and signals the success or the failure of the operation in the async result response

[source,groovy]
----
client.get(8080, "myserver.mycompany.com", "/some-uri").as(BodyCodec.pipe(writeStream)).send({ ar ->
  if (ar.succeeded()) {

    def response = ar.result()

    println("Received response with status code${response.statusCode()}")
  } else {
    println("Something went wrong ${ar.cause().getMessage()}")
  }
})

----

Finally if you are not interested at all by the response content, the `link:../../apidocs/io/vertx/ext/web/codec/BodyCodec.html#none--[BodyCodec.none]`
simply discards the entire response body

[source,groovy]
----
client.get(8080, "myserver.mycompany.com", "/some-uri").as(BodyCodec.none()).send({ ar ->
  if (ar.succeeded()) {

    def response = ar.result()

    println("Received response with status code${response.statusCode()}")
  } else {
    println("Something went wrong ${ar.cause().getMessage()}")
  }
})

----

When you don't know in advance the content type of the http response, you can still use the `bodyAsXXX()` methods
that decode the response to a specific type

[source,groovy]
----
client.get(8080, "myserver.mycompany.com", "/some-uri").send({ ar ->
  if (ar.succeeded()) {

    def response = ar.result()

    // Decode the body as a json object
    def body = response.bodyAsJsonObject()

    println("Received response with status code${response.statusCode()} with body ${body}")
  } else {
    println("Something went wrong ${ar.cause().getMessage()}")
  }
})

----

WARNING: this is only valid for the response decoded as a buffer.

=== Handling 30x redirections

By default the client follows redirections, you can configure the default behavior in the `link:../../apidocs/io/vertx/ext/web/client/WebClientOptions.html[WebClientOptions]`:

[source,groovy]
----

// Change the default behavior to not follow redirects
def client = WebClient.create(vertx, [
  followRedirects:false
])

----

The client will follow at most `16` requests redirections, it can be changed in the same options:

[source,groovy]
----

// Follow at most 5 redirections
def client = WebClient.create(vertx, [
  maxRedirects:5
])

----

== Using HTTPS

Vert.x web client can be configured to use HTTPS in exactly the same way as the Vert.x `link:../../apidocs/io/vertx/core/http/HttpClient.html[HttpClient]`.

You can specify the behavior per request

[source,groovy]
----

client.get(443, "myserver.mycompany.com", "/some-uri").ssl(true).send({ ar ->
  if (ar.succeeded()) {
    // Obtain response
    def response = ar.result()

    println("Received response with status code${response.statusCode()}")
  } else {
    println("Something went wrong ${ar.cause().getMessage()}")
  }
})

----

Or using create methods with absolute URI argument

[source,groovy]
----

client.getAbs("https://myserver.mycompany.com:4043/some-uri").send({ ar ->
  if (ar.succeeded()) {
    // Obtain response
    def response = ar.result()

    println("Received response with status code${response.statusCode()}")
  } else {
    println("Something went wrong ${ar.cause().getMessage()}")
  }
})

----

ifdef::java[]
== RxJava API

The RxJava `link:../../apidocs/io/vertx/rxjava/ext/web/client/HttpRequest.html[HttpRequest]` provides an rx-ified version of the original API,
the `link:../../apidocs/io/vertx/rxjava/ext/web/client/HttpRequest.html#rxSend--[rxSend]` method returns a `Single<HttpResponse<Buffer>>` that
makes the HTTP request upon subscription, as consequence, the `Single` can be subscribed many times.

[source,groovy]
----
Code not translatable
----

The obtained `Single` can be composed and chained naturally with the RxJava API

[source,groovy]
----
Code not translatable
----

The same APIs is available

[source,groovy]
----
Code not translatable
----

The `link:../../apidocs/io/vertx/rxjava/ext/web/client/HttpRequest.html#sendStream-rx.Observable-io.vertx.core.Handler-[sendStream]` shall
be preferred for sending bodies `Observable<Buffer>`

[source,groovy]
----
Code not translatable
----

Upon subscription, the `body` will be subscribed and its content used for the request.
endif::[]