OKHttp

OKHttp is an easy to use http library with very few dependencies. It is also the provided library that loadcoder uses to generate loadtests.

Create the OKHttp client like this:

OkHttpClient client = new OkHttpClient();

Below shows how to create and send a request, with HTTP verb, body and headers set.

RequestBody body = RequestBody.create(MediaType.get("application/x-www-form-urlencoded; charset=UTF-8"), "http body");
Request req = new Request.Builder().url("https://localhost:8080/")
	.method("POST", body)
	.header("accept", "*/*")
	.build();
	
Response resp = client.newCall(req).execute();

OkhttpClient will default follow redirects (HTTP 302). If you wish to disable this, you can do this by using the Builer methods below with the parameter false

OkHttpClient client = new OkHttpClient.Builder()
  .followSslRedirects(false)
  .followRedirects(false)
  .build

HTTPS / TLS (SSL)

If you want to call an endpoint over TLS, you need to create the RestTemplate so that it accepts the server certificate. There are a number of ways to do this.

Maven dependencies

Here are the Maven dependencies you need to use OKHttp as the client library for your load tests

<dependency>
  <groupId>com.squareup.okhttp3</groupId>
  <artifactId>okhttp-tls</artifactId>
  <version>4.2.2</version>
</dependency>