The Scala HTTP client that you always wanted!
sttp is an open-source library which provides a clean, programmer-friendly API to define HTTP requests and execute them using one of the wrapped backends, such as akka-http, async-http-client, http4s or OkHttp.
import sttp.client._
val sort: Option[String] = None
val query = "http language:scala"
// the `query` parameter is automatically url-encoded
// `sort` is removed, as the value is not defined
val request = basicRequest.get(uri"https://api.github.com/search/repositories?q=$query&sort=$sort")
implicit val backend = HttpURLConnectionBackend()
val response = request.send()
// response.header(...): Option[String]
println(response.header("Content-Length"))
// response.body: by default read into an Either[String, String] to indicate failure or success
println(response.body)
sttp (v2) documentation is available at sttp.readthedocs.io.
sttp (v1) documentation is available at sttp.readthedocs.io/en/v1.
scaladoc is available at https://www.javadoc.io
You can also take a look at the introductory blog and its follow-up.
If you are an Ammonite user, you can quickly start experimenting with sttp by copy-pasting the following:
import $ivy.`com.softwaremill.sttp.client::core:2.0.0-M11`
import sttp.client.quick._
quickRequest.get(uri"http://httpbin.org/ip").send()
This brings in the sttp API and an implicit, synchronous backend.
Add the following dependency:
"com.softwaremill.sttp.client" %% "core" % "2.0.0-M11"
Then, import:
import sttp.client._
Type sttp.
and see where your IDE’s auto-complete gets you!
If you have a question, or hit a problem, feel free to ask on our gitter channel!
Or, if you encounter a bug, something is unclear in the code or documentation, don’t hesitate and open an issue on GitHub.
We are also always looking for contributions and new ideas, so if you’d like to get into the project, check out the open issues, or post your own suggestions!
Running the tests using the JS backend has some prerequisities:
- Install Google Chrome
- Download ChromeDriver and install it
Note that running the default test
task will run the tests using both the JVM and JS backends.
If you'd like to run the tests using only the JVM backend, execute: sbt rootJVM/test
.
By default, sttp-native will not be included in the aggregate build of the root project. To include it, define the STTP_NATIVE
environmental variable before running sbt, e.g.:
STTP_NATIVE=1 sbt
You might need to install some additional libraries, see the scala native documentation site.
On MacOS, you need to: brew install llvm bdw-gc re2 libidn curl
. If your system curl is older than 7.56.0
, you'll need to use the updated brewl-curl when linking & compiling. To do that, create a core/native/local.sbt
file, and add:
nativeCompileOptions += "-I/usr/local/opt/curl/include"
nativeLinkingOptions += "-L/usr/local/opt/curl/lib"
where the specific paths correspond to the output of the brew install curl
command.
We offer commercial support for sttp and related technologies, as well as development services. Contact us to learn more about our offer!
Copyright (C) 2017-2019 SoftwareMill https://softwaremill.com.