aboutsummaryrefslogtreecommitdiff
path: root/docs/json.rst
blob: 05e524aa036e72f82d59d7b0a5e922d4415a1795 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
.. _json:

JSON
====

Adding support for JSON (or other format) bodies in requests/responses is a matter of providing a :ref:`body serializer <requestbody_custom>` and/or a :ref:`response body specification <responsebodyspec_custom>`. Both are quite straightforward to implement, so integrating with your favorite JSON library shouldn't be a problem. However, there are some integrations available out-of-the-box.

Circe
-----

JSON encoding of bodies and decoding of responses can be handled using `Circe <https://circe.github.io/circe/>`_ by the ``circe`` module. To use add the following dependency to your project::

  "com.softwaremill.sttp" %% "circe" % "1.1.10"

This module adds a method to the request and a function that can be given to a request to decode the response to a specific object::

  import com.softwaremill.sttp._
  import com.softwaremill.sttp.circe._
  
  implicit val backend = HttpURLConnectionBackend()
  
  // Assume that there is an implicit circe encoder in scope
  // for the request Payload, and a decoder for the MyResponse
  val requestPayload: Payload = ???
  
  val response: Either[io.circe.Error, MyResponse] =
    sttp
      .post(uri"...")
      .body(requestPayload)
      .response(asJson[MyResponse])
      .send()

Json4s
------

To encode and decode json using json4s-native, add the following dependency to your project::

  "com.softwaremill.sttp" %% "json4s" % "1.1.10"

Using this module it is possible to set request bodies and read response bodies as case classes, using the implicitly available ``org.json4s.Formats`` (which defaults to ``org.json4s.DefaultFormats``).

Usage example::

  import com.softwaremill.sttp._
  import com.softwaremill.sttp.json4s._
  
  implicit val backend = HttpURLConnectionBackend()

  case class Payload(...)
  case class MyResponse(...)

  val requestPayload: Payload = Payload(...)
  
  val response: Response[MyResponse] =
    sttp
      .post(uri"...")
      .body(requestPayload)
      .response(asJson[MyResponse])
      .send()