Skip to content

Commit

Permalink
Set the content encoding header
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Dec 27, 2024
1 parent ccd17c0 commit c7fb7d7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/src/main/scala/sttp/client4/requestBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ trait PartialRequestBuilder[+PR <: PartialRequestBuilder[PR, R], +R]
* @see
* [[sttp.model.Encodings]]
*/
def compressBody(encoding: String): PR = withOptions(options.copy(compressRequestBody = Some(encoding)))
def compressBody(encoding: String): PR =
withOptions(options.copy(compressRequestBody = Some(encoding))).header(HeaderNames.ContentEncoding, encoding)

/** Set the HTTP version with which this request should be sent. Supported only in a few backends. */
def httpVersion(version: HttpVersion): PR = withOptions(options.copy(httpVersion = Some(version)))
Expand Down
7 changes: 7 additions & 0 deletions core/src/test/scala/sttp/client4/RequestTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sttp.client4
import sttp.model.{Header, HeaderNames, StatusCode}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import sttp.model.Encodings

class RequestTests extends AnyFlatSpec with Matchers {

Expand Down Expand Up @@ -108,4 +109,10 @@ class RequestTests extends AnyFlatSpec with Matchers {
Header("Content-Length", "4")
)
}

"compressBody" should "add the content encoding header" in {
emptyRequest.compressBody(Encodings.Gzip).headers.toSet shouldBe Set(
Header(HeaderNames.ContentEncoding, Encodings.Gzip)
)
}
}

0 comments on commit c7fb7d7

Please sign in to comment.