diff options
author | adamw <adam@warski.org> | 2017-11-22 15:44:56 +0100 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-11-22 15:44:56 +0100 |
commit | ae5876b86b8e12ee182e730c88aa690871d3fe67 (patch) | |
tree | 1284f790aa6b6af02ad475ea3fd4558d6c6ae5fe /docs/examples.rst | |
parent | fe50ad485c037c30dada741cf87e552a99c2d9bb (diff) | |
download | sttp-ae5876b86b8e12ee182e730c88aa690871d3fe67.tar.gz sttp-ae5876b86b8e12ee182e730c88aa690871d3fe67.tar.bz2 sttp-ae5876b86b8e12ee182e730c88aa690871d3fe67.zip |
Docs: usage examples
Diffstat (limited to 'docs/examples.rst')
-rw-r--r-- | docs/examples.rst | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/docs/examples.rst b/docs/examples.rst new file mode 100644 index 0000000..397be22 --- /dev/null +++ b/docs/examples.rst @@ -0,0 +1,96 @@ +.. _usage_examples: + +Usage examples +============== + +POST a form using the synchronous backend +----------------------------------------- + +Required dependencies:: + + libraryDependencies ++= List("com.softwaremill.sttp" %% "core" % "1.0.6") + +Example code:: + + import com.softwaremill.sttp._ + + val signup = Some("yes") + + val request = sttp + // send the body as form data (x-www-form-urlencoded) + .body(Map("name" -> "John", "surname" -> "doe")) + // use an optional parameter in the URI + .post(uri"https://httpbin.org/post?signup=$signup") + + implicit val backend = HttpURLConnectionBackend() + val response = request.send() + + println(response.body) + println(response.headers) + +GET and parse JSON using the akka-http backend and json4s +--------------------------------------------------------- + +Required dependencies:: + + libraryDependencies ++= List( + "com.softwaremill.sttp" %% "akka-http-backend" % "1.0.6", + "com.softwaremill.sttp" %% "json4s" % "1.0.6" + ) + +Example code:: + + import com.softwaremill.sttp._ + import com.softwaremill.sttp.akkahttp._ + import com.softwaremill.sttp.json4s._ + + import scala.concurrent.ExecutionContext.Implicits.global + + case class HttpBinResponse(origin: String, headers: Map[String, String]) + + val request = sttp + .get(uri"https://httpbin.org/get") + .response(asJson[HttpBinResponse]) + + implicit val backend = AkkaHttpBackend() + val response: Future[Response[HttpBinResponse]] = request.send() + + for { + r <- response + } { + println(s"Got response code: ${r.code}") + println(r.body) + backend.close() + } + +Test an endpoint requiring multiple parameters +---------------------------------------------- + +Required dependencies:: + + libraryDependencies ++= List("com.softwaremill.sttp" %% "core" % "1.0.6") + +Example code:: + + import com.softwaremill.sttp._ + import com.softwaremill.sttp.testing._ + + implicit val backend = SttpBackendStub.synchronous + .whenRequestMatches(_.uri.paramsMap.contains("filter")) + .thenRespond("Filtered") + .whenRequestMatches(_.uri.path.contains("secret")) + .thenRespond("42") + + val parameters1 = Map("filter" -> "name=mary", "sort" -> "asc") + println( + sttp + .get(uri"http://example.org?search=true&$parameters1") + .send() + .unsafeBody) + + val parameters2 = Map("sort" -> "desc") + println( + sttp + .get(uri"http://example.org/secret/read?$parameters2") + .send() + .unsafeBody)
\ No newline at end of file |