You can require the entire toolkit in a single line:
//> using toolkit latest
Alternatively, you can require just a specific version of sttp:
//> using dep com.softwaremill.sttp.client4::core:4.0.0-M6
In your build.sbt file, you can add a dependency on the Toolkit:
lazy val example = project.in(file("."))
.settings(
scalaVersion := "3.3.4",
libraryDependencies += "org.scala-lang" %% "toolkit" % "0.1.7"
)
Alternatively, you can require just a specific version of sttp:
libraryDependencies += "com.softwaremill.sttp.client4" %% "core" % "4.0.0-M6"
In your build.sc file, you can add a dependency on the Toolkit:
object example extends ScalaModule {
def scalaVersion = "3.3.4"
def ivyDeps =
Agg(
ivy"org.scala-lang::toolkit:0.1.7"
)
}
Alternatively, you can require just a specific version of sttp:
ivy"com.softwaremill.sttp.client4::core:4.0.0-M6"
Uploading a file
To upload a file, you can put a Java Path
in the body of a request.
You can get a Path
directly using Paths.get("path/to/file")
or by converting an OS-Lib path to a Java path with toNIO
.
import sttp.client4.quick._
val file: java.nio.file.Path = (os.pwd / "image.png").toNIO
val response = quickRequest.post(uri"https://example.com/").body(file).send()
println(response.code)
// prints: 200
import sttp.client4.quick.*
val file: java.nio.file.Path = (os.pwd / "image.png").toNIO
val response = quickRequest.post(uri"https://example.com/").body(file).send()
println(response.code)
// prints: 200
Multi-part requests
If the web server can receive multiple files at once, you can use a multipart body, as follows:
import sttp.client4.quick._
val file1 = (os.pwd / "avatar1.png").toNIO
val file2 = (os.pwd / "avatar2.png").toNIO
val response = quickRequest
.post(uri"https://example.com/")
.multipartBody(
multipartFile("avatar1.png", file1),
multipartFile("avatar2.png", file2)
)
.send()
import sttp.client4.quick.*
val file1 = (os.pwd / "avatar1.png").toNIO
val file2 = (os.pwd / "avatar2.png").toNIO
val response = quickRequest
.post(uri"https://example.com/")
.multipartBody(
multipartFile("avatar1.png", file1),
multipartFile("avatar2.png", file2)
)
.send()
Learn more about multipart requests in the sttp documention.