diff options
Diffstat (limited to 'core/src/main/scala/com/softwaremill/sttp/RequestT.scala')
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/RequestT.scala | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/RequestT.scala b/core/src/main/scala/com/softwaremill/sttp/RequestT.scala index ff80dd5..b785e7c 100644 --- a/core/src/main/scala/com/softwaremill/sttp/RequestT.scala +++ b/core/src/main/scala/com/softwaremill/sttp/RequestT.scala @@ -100,10 +100,8 @@ case class RequestT[U[_], T, +S]( * bytes in the string using the given encoding. */ def body(b: String, encoding: String): RequestT[U, T, S] = - setContentTypeIfMissing( - contentTypeWithEncoding(TextPlainContentType, encoding)) + withBasicBody(StringBody(b, encoding)) .setContentLengthIfMissing(b.getBytes(encoding).length) - .copy(body = StringBody(b, encoding)) /** * If content type is not yet specified, will be set to @@ -113,25 +111,22 @@ case class RequestT[U[_], T, +S]( * of the given array. */ def body(b: Array[Byte]): RequestT[U, T, S] = - setContentTypeIfMissing(ApplicationOctetStreamContentType) + withBasicBody(ByteArrayBody(b)) .setContentLengthIfMissing(b.length) - .copy(body = ByteArrayBody(b)) /** * If content type is not yet specified, will be set to * `application/octet-stream`. */ def body(b: ByteBuffer): RequestT[U, T, S] = - setContentTypeIfMissing(ApplicationOctetStreamContentType).copy( - body = ByteBufferBody(b)) + withBasicBody(ByteBufferBody(b)) /** * If content type is not yet specified, will be set to * `application/octet-stream`. */ def body(b: InputStream): RequestT[U, T, S] = - setContentTypeIfMissing(ApplicationOctetStreamContentType).copy( - body = InputStreamBody(b)) + withBasicBody(InputStreamBody(b)) /** * If content type is not yet specified, will be set to @@ -151,9 +146,8 @@ case class RequestT[U[_], T, +S]( * of the given file. */ def body(b: Path): RequestT[U, T, S] = - setContentTypeIfMissing(ApplicationOctetStreamContentType) + withBasicBody(PathBody(b)) .setContentLengthIfMissing(b.toFile.length()) - .copy(body = PathBody(b)) /** * Encodes the given parameters as form data using `utf-8`. @@ -204,13 +198,10 @@ case class RequestT[U[_], T, +S]( * `application/octet-stream`. */ def body[B: BodySerializer](b: B): RequestT[U, T, S] = - setContentTypeIfMissing(ApplicationOctetStreamContentType).copy( - body = SerializableBody(implicitly[BodySerializer[B]], b)) - - //def multipartData(parts: MultiPart*): RequestTemplate[U] = ??? + withBasicBody(implicitly[BodySerializer[B]].apply(b)) def streamBody[S2 >: S](b: S2): RequestT[U, T, S2] = - this.copy[U, T, S2](body = StreamBody(b)) + copy[U, T, S2](body = StreamBody(b)) /** * What's the target type to which the response body should be read. @@ -239,6 +230,17 @@ case class RequestT[U[_], T, +S]( private def setContentTypeIfMissing(ct: String): RequestT[U, T, S] = if (hasContentType) this else contentType(ct) + private def withBasicBody(body: BasicRequestBody) = { + val defaultCt = body match { + case StringBody(_, encoding, Some(ct)) => + Some(contentTypeWithEncoding(ct, encoding)) + case _ => + body.defaultContentType + } + + defaultCt.fold(this)(setContentTypeIfMissing).copy(body = body) + } + private def hasContentLength: Boolean = headers.exists(_._1.equalsIgnoreCase(ContentLengthHeader)) private def setContentLengthIfMissing(l: => Long): RequestT[U, T, S] = @@ -247,10 +249,11 @@ case class RequestT[U[_], T, +S]( private def formDataBody(fs: Seq[(String, String)], encoding: String): RequestT[U, T, S] = { val b = fs - .map( - p => - URLEncoder.encode(p._1, encoding) + "=" + URLEncoder - .encode(p._2, encoding)) + .map { + case (key, value) => + URLEncoder.encode(key, encoding) + "=" + + URLEncoder.encode(value, encoding) + } .mkString("&") setContentTypeIfMissing(ApplicationFormContentType) .setContentLengthIfMissing(b.getBytes(encoding).length) |