0PricingLogin
Scala for Backend Engineering & Functional Programming · Lesson

Requests and Responses

Read input and return output.

Request as a Value

In http4s a Request[F] is an immutable value carrying the method, URI, headers, HTTP version, and a streaming body. You inspect it with pure accessors and never mutate it in place.

To change a request you produce a copy via withHeaders, withUri, or similar combinators.

val r: Request[IO] = Request[IO](
  method = Method.GET,
  uri = uri"/health"
)

The Streaming Body

The body of a request or response is an fs2.Stream[F, Byte]. It is consumed lazily and effectfully, so large payloads never have to sit fully in memory.

You usually do not touch raw bytes; an EntityDecoder handles decoding for you.

// raw access (rarely needed)
val bytes: fs2.Stream[IO, Byte] = request.body

All lessons in this course

  1. Routes and HttpRoutes
  2. Requests and Responses
  3. JSON Endpoints
  4. Serving the Application
← Back to Scala for Backend Engineering & Functional Programming