Synchronous backends¶
There are several synchronous backend implementations. Sending a request using these backends is a blocking operation, and results in a sttp.client.Response[T]
.
Using HttpURLConnection¶
The default synchronous backend, available in the main jar for the JVM.
To use, add an implicit value:
implicit val sttpBackend = HttpURLConnectionBackend()
Using OkHttp¶
To use, add the following dependency to your project:
"com.softwaremill.sttp.client" %% "okhttp-backend" % "2.0.0-RC9"
Create the backend using:
import sttp.client.okhttp.OkHttpSyncBackend
implicit val sttpBackend = OkHttpSyncBackend()
// or, if you'd like to instantiate the OkHttpClient yourself:
implicit val sttpBackend = OkHttpSyncBackend.usingClient(okHttpClient)
This backend depends on OkHttp and fully supports HTTP/2.
Using HttpClient (Java 11+)¶
To use, add the following dependency to your project:
"com.softwaremill.sttp.client" %% "httpclient-backend" % "2.0.0-RC9"
Create the backend using:
import sttp.client.httpclient.HttpClientSyncBackend
implicit val sttpBackend = HttpClientSyncBackend()
// or, if you'd like to instantiate the HttpClient yourself:
implicit val sttpBackend = HttpClientSyncBackend.usingClient(httpClient)
This backend is based on the built-in java.net.http.HttpClient
available from Java 11 onwards.
Websockets¶
The HttpURLConnection
-based backend doesn’t support websockets.
OkHttp and HttpClient backends support websockets by wrapping a low-level Java interface:
sttp.client.okhttp.WebSocketHandler
, orsttp.client.httpclient.WebSocketHandler