-
Request & URIscala/sttp 2023. 7. 18. 15:09
sttp 사용법 소개한다.
공식 메뉴얼 대신에 scala toolkit 에 나오는 간단한 예제를 옮겨봤다.
libraryDependencies += "com.softwaremill.sttp.client4" %% "core" % "4.0.0-M2"
object SttpMain { def main(args: Array[String]): Unit = { import sttp.client4.Response import sttp.client4.quick._ val response: Response[String] = quickRequest .get(uri"https://httpbin.org/get") .send() println(response.code) println(response.body) //////////////////////////////////////////////////////// // URI interpolation //////////////////////////////////////////////////////// import sttp.model.Uri val book = "programming in scala" val bookUri: Uri = uri"https://example.com/books/$book" println(bookUri) // https://example.com/books/programming%20in%20scala //////////////////////////////////////////////////////// val queryParams = Map( "q" -> "scala", "limit" -> "10", "page" -> "1" ) val uriWithQueryParams = uri"https://example.com/search?$queryParams" println(uriWithQueryParams) // prints: https://example.com/search?q=scala&limit=10&page=1 //////////////////////////////////////////////////////// def getUri(limit: Option[Int]): Uri = uri"https://example.com/all?limit=$limit" println(getUri(Some(10))) // prints: https://example.com/all?limit=100 println(getUri(None)) // prints: https://example.com/all } }