Scala encodes the multipart / form-data body

advertisements

I'm trying to send file in multipart/form-data request.

My problem is that on server side (in my case PHP) file contents are twice bigger than expected.

How should I convert the data?

My code is

val boundary = randomBoundary

val CrLf = "\r\n"
val body = (
  for (fileInfo ← files)
  yield s"--$boundary${CrLf}Content-Disposition: form-data; name=AttachedFile1; filename=${fileInfo.fileName}$CrLf$CrLf" +
    s"Content-Type: application/octet-stream$CrLf$CrLf" +
    new String(fileInfo.data) // fileInfo.data has type Array[Byte]
).mkString(CrLf) + s"$CrLf--$boundary--"


Don't use Strings to send binary data; Strings are UTF-8 encoded character data on the JVM.

You want an Array[Byte], which is easily emitted (to a target server) by any java.io.OutputStream.