Skip to main content
Version: 2.0.x

Response

ZIO HTTP Response is designed to encode HTTP Response. It supports all HTTP status codes and headers along with custom methods and headers (as defined in RFC2616 )

Creating a Response

Response can be created with status, headers and data.

The below snippet creates a response with default params, status as Status.OK, headers as Headers.empty and data as Body.Empty.

import zio.http._
import zio._

Response()
// res0: Response = Response(
// status = Ok,
// headers = Iterable(),
// body = Body.empty
// )

Empty Response

ok creates an empty response with status code 200

Response.ok
// res1: Response = Response(
// status = Ok,
// headers = Iterable(),
// body = Body.empty
// )

status creates an empty response with provided status code.

Response.status(Status.Continue)
// res2: Response = Response(
// status = Continue,
// headers = Iterable(),
// body = Body.empty
// )

Specialized Response Constructors

text creates a response with data as text, content-type header set to text/plain and status code 200

Response.text("hey")
// res3: Response = Response(
// status = Ok,
// headers = Iterable(Custom(customName = "content-type", value = "text/plain")),
// body = AsciiStringBody(asciiString = hey, mediaType = None, boundary = None)
// )

json creates a response with data as json, content-type header set to application/json and status code 200

Response.json("""{"greetings": "Hello World!"}""")
// res4: Response = Response(
// status = Ok,
// headers = Iterable(
// Custom(customName = "content-type", value = "application/json")
// ),
// body = AsciiStringBody(
// asciiString = {"greetings": "Hello World!"},
// mediaType = None,
// boundary = None
// )
// )

html creates a response with data as html, content-type header set to text/html and status code 200

import zio.http.template._

Response.html(Html.fromString("html text"))
// res5: Response = Response(
// status = Ok,
// headers = Iterable(Custom(customName = "content-type", value = "text/html")),
// body = AsciiStringBody(
// asciiString = <!DOCTYPE html>html text,
// mediaType = None,
// boundary = None
// )
// )

Specialized Response Operators

status to update the status of Response

Response.text("Hello World!").status(Status.NOT_FOUND)

updateHeaders to update the headers of Response

Response.ok.updateHeaders(_ => Headers("key", "value"))
// res6: Response = Response(
// status = Ok,
// headers = Iterable(Custom(customName = "key", value = "value")),
// body = Body.empty
// )

Response from Http Errors

error creates a response with a provided status code and message.

Response.error(Status.BadRequest, "It's not good!")
// res7: Response = Response(
// status = BadRequest,
// headers = Iterable(
// Warning(
// code = 400,
// agent = "ZIO HTTP",
// text = "It&#x27;s not good!",
// date = None
// )
// ),
// body = Body.empty
// )

addCookie adds cookies in the headers of the response.

val cookie = Cookie.Response("key", "value")
// cookie: Cookie.Response = Response(
// name = "key",
// content = "value",
// domain = None,
// path = None,
// isSecure = false,
// isHttpOnly = false,
// maxAge = None,
// sameSite = None
// )
Response.ok.addCookie(cookie)
// res8: Response = Response(
// status = Ok,
// headers = Iterable(
// SetCookie(
// value = Response(
// name = "key",
// content = "value",
// domain = None,
// path = None,
// isSecure = false,
// isHttpOnly = false,
// maxAge = None,
// sameSite = None
// )
// )
// ),
// body = Body.empty
// )