Skip to content

Commit 33ff10d

Browse files
Provides a custom JSON BodyPart for Circe
1 parent 6c8b062 commit 33ff10d

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

client/shared/src/main/scala/org/scalaexercises/evaluator/http/HttpClient.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package org.scalaexercises.evaluator.http
88
import io.circe.Decoder
99
import org.scalaexercises.evaluator.EvaluatorResponses
1010
import org.scalaexercises.evaluator.EvaluatorResponses.EvaluationResponse
11+
1112
import scala.concurrent.Future
1213

1314
object HttpClient {

client/shared/src/main/scala/org/scalaexercises/evaluator/http/HttpRequestBuilder.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import org.scalaexercises.evaluator.http.HttpClient._
99

1010
import scala.concurrent.Future
1111

12-
import fr.hmil.roshttp.body.Implicits._
1312
import fr.hmil.roshttp.{HttpRequest, Method, HttpResponse}
14-
import fr.hmil.roshttp.body.JSONBody
13+
import fr.hmil.roshttp.body.BodyPart
14+
15+
import java.nio.ByteBuffer
1516

1617
case class HttpRequestBuilder(
1718
url: String,
@@ -20,6 +21,12 @@ case class HttpRequestBuilder(
2021
body: String = ""
2122
) {
2223

24+
case class CirceJSONBody(value: String) extends BodyPart {
25+
override def contentType: String = s"application/json; charset=utf-8"
26+
27+
override def content: ByteBuffer = ByteBuffer.wrap(value.getBytes("utf-8"))
28+
}
29+
2330
def withHeaders(headers: Headers) = copy(headers = headers)
2431

2532
def withBody(body: String) = copy(body = body)
@@ -28,9 +35,9 @@ case class HttpRequestBuilder(
2835

2936
val request = HttpRequest(url)
3037
.withMethod(Method(httpVerb))
38+
.withHeader("content-type", "application/json")
3139
.withHeaders(headers.toList: _*)
32-
.withHeader("content.type", "application/json")
3340

34-
request.post(JSONBody(body))
41+
request.send(CirceJSONBody(body))
3542
}
3643
}

0 commit comments

Comments
 (0)