Created
November 18, 2017 17:27
-
-
Save labra/d04d3093c8daef9a8e9be3d4731fd817 to your computer and use it in GitHub Desktop.
http4s example processing multipart content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.http4sfile | |
import cats.effect.IO | |
import org.http4s._ | |
import org.http4s.dsl.Http4sDsl | |
import org.http4s.multipart.{Multipart, Part} | |
import org.http4s.server.blaze.BlazeBuilder | |
import org.http4s.util.StreamApp | |
import org.http4s.MediaType._ | |
import org.http4s.headers._ | |
object HelloWorldServer extends StreamApp[IO] with Http4sDsl[IO] { | |
val service = HttpService[IO] { | |
case GET -> Root => | |
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> | |
""".stripMargin). | |
withContentType(Some(`Content-Type`(`text/html`))) | |
case req @ POST -> Root / "post" => { | |
println(s"POST request: $req") | |
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) | |
} | |
} | |
} | |
def stream(args: List[String], requestShutdown: IO[Unit]) = | |
BlazeBuilder[IO] | |
.bindHttp(8080, "0.0.0.0") | |
.mountService(service, "/") | |
.serve | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment