softwaremill / sttp

The Scala HTTP client you always wanted!
https://sttp.softwaremill.com
Apache License 2.0
1.43k stars 299 forks source link

Request body gzip compression #1918

Open nikola-mladenovic opened 11 months ago

nikola-mladenovic commented 11 months ago

Hi,

Do you have a built-in mechanism for gzip compressing request body? So far I've tried the following:

def gzipCompressBytes(dataBytes: Array[Byte]): Array[Byte] = {
  val baos = new ByteArrayOutputStream
  val gzos = new GZIPOutputStream(baos)
  gzos.write(dataBytes)
  gzos.finish()
  gzos.close()
  baos.close()
  baos.toByteArray
}

val body: JValue = JArray(objs.toList)
val compressedBody = gzipCompressBytes(body.show.getBytes())

val baseRequest = basicRequest
 .header(ContentEncoding, "gzip")
 .header(ContentType, "application/json")
 .body(compressedBody)
 .header("accept", "application/json")

However this approach didn't seem to work - is there any recommendation from your end how to achieve request body compression? Thanks! 🙏

adamw commented 10 months ago

So far there's no built-in mechanism, but you are right that this is a good candidate for a feature (just as we have auto-decompression).

Until this is done, your "manual" approach looks fine. I tested it with https://postman-echo.com/post and got back correct results. Can you describe what problems do you encounter, and which backend you are using?

iyfedorov commented 2 months ago

2148