lihaoyi / test

0 stars 0 forks source link

BUG? - Multipart upload removes content-length #659

Open lihaoyi opened 1 month ago

lihaoyi commented 1 month ago

According to your code, you need to calculate content-length for multi-part uploads. After doing that, you specifically upload multipart as chunked data with no content-length. This has led to issues for me exactly because of the reason stated in line 155.

when i try to POST, content-length is removed from the headers

If i make a post with:


    val session: Session = requests.Session(
      headers = Map(
        "user-agent" -> userAgentHeader,
        "authorization" -> authHeader,
        "accept" -> "*/*",
        "content-type" -> "application/json; charset=utf-8"
      )
    )
val file = new File("path-to-file")
val multiPart = MultiPart(MultiItem("file", file, file.getName))
val headers = Map(
    "thing" -> "thang",
     "Content-Length" -> multiPart.totalBytesToSend.toString,
      "content-type" -> s"multipart/form-data; boundary=${multiPart.boundary}"
)
session.post("a url", data = multiPart, headers = headers)

When I POST to myself and look at the headers, the content-length is gone, because it is controlled at the Java level here

My current workaround is this

case class SizedMultiPart(items: requests.MultiItem*)
    extends requests.RequestBlob.MultipartFormRequestBlob(items)
    with requests.RequestBlob.SizedBlob {

  override def length: Long = totalBytesToSend

  override def inMemory: Boolean = false
  override def headers: Seq[(String, String)] = Seq(
    "Content-Type" -> s"multipart/form-data; boundary=$boundary",
    "Content-Length" -> totalBytesToSend.toString
  )
}

After going through all the trouble of calculating size for MultiPartFormRequestBlob, is there a reason why that is then discarded by making MultiPart not a SizedRequestBlob?
ID: 42 Original Author: eric-s-s