Creating and configuring a client
In this example, we use the CIO engine. You can also omit an engine:
In this case, the client will choose an engine automatically depending on the artifacts added in a build script. You can learn how the client chooses an engine from the Default engine documentation section.
Configure the client
To configure the client, you can pass an additional functional parameter to the client constructor. The HttpClientConfig class is a base class for configuring the client. For instance, you can enable response validation using the
You can configure an engine using the
See the Engines section for additional details.
You can also configure a plugin inside the
install block. For example, for the Logging plugin, you can specify the logger and logging level:
Note that a specific plugin might require a separate dependency.
Use the client
Close the client
After you finish working with the HTTP client, you need to free up the resources: threads, connections, and
CoroutineScope for coroutines. To do this, call the
Note that the
close function prohibits creating new requests but doesn't terminate currently active ones. Resources will only be released after all client requests are completed.
If you need to use
HttpClient for a single request, call the
use function, which automatically calls
close after executing the code block: