I would like to know how can I process multipart content using the http4s library.
Imagine a service with the following snippet (the complete gist is here):
case GET -> Root / "form" =>
Ok(
"""|<html>
|<body>
|<form method="post" action="/post" enctype="multipart/form-data">
| <input type="date" name="birthDate" placeholder="birthDate">
| <input type="file" name="dataFile">
| <input type="submit">
|</form></body></html>""".stripMargin).
withContentType(Some(`Content-Type`(`text/html`)))
case req @ POST -> Root / "post" => {
req.decode[Multipart[IO]] { m =>
Ok(
s"""Multipart Data\nParts:${m.parts.length}
|${m.parts.map { case f: Part[IO] => { f.name + ", headers: " + f.headers.mkString(",")} }.mkString("\n")}""".stripMargin)
}
}
If I execute the service and fill the corresponding fields, I obtain an output like the following:
Multipart Data
Parts:2
Some(birthDate), headers: Content-Disposition: form-data; name="birthDate"
Some(dataFile), headers: Content-Disposition: form-data; name="dataFile";
filename="file.pdf",Content-Type: application/pdf
So I know how to obtain information about the parts, which are elements of type Part[IO]
and contain headers
and body
.
What I would like is to know how to process those parts. In this case, for example, I would like to open the file and inform about its length. What is the idiomatic way to do that?