Mysterious chunking behaviour when using Akka HTTP

Hi everyone! When using chunked encoding I notice that I’m getting a chunk of len(1) as the first chunk. It could of course be something I’m doing, but I wasn’t expecting this. Any ideas? My test code:

test("client") {
  Post(
    "/consumers/testgroup",
    JsObject("subscriptions" -> JsArray(JsObject("topic" -> JsString(topic1))))
  ).withHeaders(
    `Accept`(MediaTypes.`application/json`),
    `Transfer-Encoding`(TransferEncodings.chunked),
  ) ~> MyHttpRoutes() ~> check {
    status == StatusCodes.OK
    contentType ==> ContentTypes.`application/json`

    val firstDataChunk = chunksStream
      .drop(1) // FIXME: Understand why this must be dropped - there's an element with a len of 1 - is this just the way chunked encoding works?
      .runWith(Sink.head)
    firstDataChunk.map(
      _.data().utf8String.parseJson ==> JsObject(
        "key"       -> JsNumber(1),
        "offset"    -> JsNumber(0),
        "partition" -> JsNumber(0),
        "topic"     -> JsString(ns + ":" + topic1),
        "value"     -> JsString(data1.encodeBase64.toString())
      )
    )
  }
}

Thanks in advance!

I worked it out. :-) JSON was being returned as an array. The first chunk was the start of array marker ("[").

Switching to JSON lines output as described here works just fine.

2 Likes