Questions tagged [http4s]

Http4s is a minimal, idiomatic Scala interface for HTTP services.

http4s is a typeful, purely functional HTTP library for client and server applications written in Scala.

Principles

Typeful: http4s uses Scala’s type system to increase self-documentation and compile-time verification. Standard headers are lazily parsed to semantically meaningful types, and typeclasses are provided to encode and decode bodies to several common formats.

Purely functional: The pure functional side of Scala is favored to promote composability and easy reasoning about your code. The core is built on an immutable case class model of HTTP requests and responses, shared by the client and the server.

Asynchronous: Much of the API is built around a scalaz.concurrent.Task. Bodies are modeled as scalaz-streams for performant chunking of large messages in constant memory.

Modular: http4s has a lightweight core with multiple deployment options. Server applications can be deployed to blaze, the native platform, or as a servlet application. Client applications run on either blaze or an async-http-client backend. Several libraries useful in everyday HTTP programming, such as circe and argonaut, are integrated via optional modules.

Community-oriented: http4s is a community-driven project, and aims to provide a welcoming environment for all users. We are proud to be a Typelevel incubator project.

Official website: http://http4s.org/

161 questions
5
votes
2 answers

What is the right way to send JSON response in http4s?

Not so long time ago I switched from akka-http to http4s. One of the basic things which I wanted to do correctly — JSON handling, in particular sending a JSON response. I decided to use http4s with ZIO instead of cats, so here is how an http route…
Alex Fruzenshtein
  • 2,846
  • 6
  • 32
  • 53
5
votes
2 answers

Scala, cats, http4s - does not recognize <+> symbol from Http4s

I have created two Http4s routes: class FirstRoutes[F[_] : Async](service: FirstService[F]) extends Http4sDsl[F] { def routes: HttpRoutes[F] = HttpRoutes.of[F] { //... some code } } class SecondRoutes[F[_] : Async] extends…
Developus
  • 1,400
  • 2
  • 14
  • 50
5
votes
1 answer

Scala: http4s giving 401 Unauthorized for same request that works in curl/requests

I tried the following code using http4s v0.19.0: import cats.effect._ def usingHttp4s(uri: String, bearerToken: String)(implicit cs: ContextShift[IO]): String = { import scala.concurrent.ExecutionContext import org.http4s.client.dsl.io._ …
pathikrit
  • 32,469
  • 37
  • 142
  • 221
5
votes
1 answer

How do I turn a cats IO into a Effect using http4s

I've got some code that returns an IO but I need a Effect in http4s. import cats.effect.{Effect, IO} class Service[F[_]: Effect] extends Http4sDsl[F] { val service: HttpService[F] = { HttpService[F] { case GET -> Root => …
Stephen
  • 4,228
  • 4
  • 29
  • 40
4
votes
0 answers

Scala Customizing Refined Type Exceptions in Http4s

I am using refined for my requests in order to ensure that the request body confronts to certain specs, such as case class NewGenreRequest( name: GenreNameParam, parent: Option[GenreParentParam] ) where type GenreNameString = String…
sinanspd
  • 2,589
  • 3
  • 19
  • 37
4
votes
0 answers

Combining Routes in Http4S

Browsing the doc i see: Multiple HttpRoutes can be combined with the combineK method (or its alias <+>) tweetService <+> helloWorldService I wonder what it is the outcome of that ? i.e. Combing to Kleisli with CombineK…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
4
votes
1 answer

Need clarification for confusing Http4s Message Type `Response[F]` / `Request[F]`

I have a hard time understanding why Request and Response are parameterized in F. Taking something similar is the cats effect datatype Resource. From the documentation https://typelevel.org/cats-effect/docs/std/resource We find the following…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
4
votes
2 answers

Integration testing an HTTP server with ZIO test suite

I am trying to figure out the idiom for writing an integration test for an Http4s app that supports two end points. I'm starting the Main app class in a ZManaged by forking it on a new fiber and then doing interruptFork on release of the ZManaged. I…
arinray
  • 178
  • 1
  • 12
4
votes
1 answer

Building a ZIO and http4s app, works with sbt, fails with Bazel: missing an implicit

I'm attempting to build a service that integrates ZIO and http4s. The starting point is this example (it uses zio 1.0.1, http4s 0.21.3, scala 2.12.11) I was able to build the code below without any problems using sbt, but am running into trouble…
Shastick
  • 1,218
  • 1
  • 12
  • 29
4
votes
0 answers

How to chain routes correctly

I have defined the following route: object InterestRoutes { def apply[F[_]: Sync](query: InterestProgram[F]): InterestRoutes[F] = new InterestRoutes[F](query) } final class InterestRoutes[F[_]: Sync](query: InterestProgram[F]) extends…
softshipper
  • 32,463
  • 51
  • 192
  • 400
4
votes
0 answers

How do I handle connection pool errors in http4s?

I'm trying to respond back with a dummy response if http4s's blaze client cannot connect to a service, but I'm unable to figure out how to handle pooling errors. Setting up a connection pool import java.util.concurrent._ private val…
Shubham Kanodia
  • 6,036
  • 3
  • 32
  • 46
4
votes
1 answer

"Spawn" concurrent effect in a WebSocket endpoint

I have the following code: class ApiRoutes2[F[_]](implicit F: ConcurrentEffect[F]) extends Http4sDsl[F] { var queue = Queue.bounded[F, String](100) def createService(queue: Queue[F, String]): F[Unit] = ??? val service: HttpRoutes[F] =…
Simão Martins
  • 1,210
  • 11
  • 22
4
votes
1 answer

Model multiple function calls with a stream (in a safe, FP way)

Given a function A => IO[B] (aka Kleisli[IO, A, B]) that is meant to be called multiple times, and has side effects, like updating a DB, how to delegate such multiple calls of it into a stream (I guess Pipe[IO, A, B]) (fs2, monix…
V-Lamp
  • 1,630
  • 10
  • 18
4
votes
1 answer

How to use http4s server and client library as a proxy?

I want use http4s as proxy(like nginx), how to forward all data from my http4s server to another http server? What I really want do is append a verify function on every request before do forward function. Hopefully like this: HttpService[IO] { …
LoranceChen
  • 2,453
  • 2
  • 22
  • 48
4
votes
1 answer

Processing multipart content in http4s

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( """| | |
Labra
  • 1,412
  • 1
  • 13
  • 33
1
2
3
10 11