Documentation

You are viewing the documentation for the 3.0.3 release. The latest stable release series is 3.0.x.

§Manipulating Results

§Changing the default Content-Type

The result content type is automatically inferred from the Java value you specify as response body.

For example:

Result textResult = ok("Hello World!");

Will automatically set the Content-Type header to text/plain, while:

JsonNode json = Json.toJson(object);
Result jsonResult = ok(json);

will set the Content-Type header to application/json.

This is pretty useful, but sometimes you want to change it. Just use the as(newContentType) method on a result to create a new similar result with a different Content-Type header:

Result htmlResult = ok("<h1>Hello World!</h1>").as("text/html");

or even better, using:

Result htmlResult = ok("<h1>Hello World!</h1>").as(MimeTypes.HTML);

§Manipulating HTTP headers

You can also add (or update) any HTTP header to the result:

public Result index() {
  return ok("<h1>Hello World!</h1>")
      .as(MimeTypes.HTML)
      .withHeader(CACHE_CONTROL, "max-age=3600")
      .withHeader(ETAG, "some-etag-calculated-value");
}

Note that setting an HTTP header will automatically discard the previous value if it was existing in the original result.

§Setting and discarding cookies

Cookies are just a special form of HTTP headers, but Play provides a set of helpers to make it easier.

You can easily add a Cookie to the HTTP response using:

public Result index() {
  return ok("<h1>Hello World!</h1>")
      .as(MimeTypes.HTML)
      .withCookies(Cookie.builder("theme", "blue").build());
}

If you need to set more details, including the path, domain, expiry, whether it’s secure, and whether the HTTP only flag should be set, you can do this with the overloaded methods:

public Result index() {
  return ok("<h1>Hello World!</h1>")
      .as(MimeTypes.HTML)
      .withCookies(
          Cookie.builder("theme", "blue")
              .withMaxAge(Duration.ofSeconds(3600))
              .withPath("/some/path")
              .withDomain(".example.com")
              .withSecure(false)
              .withHttpOnly(true)
              .withSameSite(Cookie.SameSite.STRICT)
              .build());
}

To discard a Cookie previously stored on the web browser:

public Result index() {
  return ok("<h1>Hello World!</h1>").as(MimeTypes.HTML).discardingCookie("theme");
}

If you set a path or domain when setting the cookie, make sure that you set the same path or domain when discarding the cookie, as the browser will only discard it if the name, path and domain match.

§Changing the charset for text based HTTP responses

For a text based HTTP response it is very important to handle the charset correctly. Play handles that for you and uses utf-8 by default (see why to use utf-8).

The charset is used to both convert the text response to the corresponding bytes to send over the network socket, and to update the Content-Type header with the proper ;charset=xxx extension.

The encoding can be specified when you are generating the Result value:

public Result index() {
  return ok("<h1>Hello World!</h1>", "iso-8859-1")
      .as("text/html; charset=iso-8859-1");
}

§Range Results

Play supports part of RFC 7233 which defines how range requests and partial responses works. It enables you to deliver a 206 Partial Content if a satisfiable Range header is present in the request. It will also returns a Accept-Ranges: bytes for the delivered Result.

Note: Besides the fact that some parsing is done to better handle multiple ranges, multipart/byteranges is not fully supported yet.

Range results can be generated for a Source, InputStream, File, and Path. See RangeResult API documentation for see all the methods available. For example:

public Result index(Http.Request request) {
  String content = "This is the full content!";
  InputStream input = getInputStream(content);
  return RangeResults.ofStream(request, input, content.length());
}

Or for an Source:

public Result index(Http.Request request) {
  String content = "This is the full content!";
  Source<ByteString, NotUsed> source = sourceFrom(content);
  return RangeResults.ofSource(
      request, (long) content.length(), source, "file.txt", MimeTypes.TEXT);
}

When the request Range is not satisfiable, for example, if the range in the request’s Range header field do not overlap the current extent of the selected resource, then a HTTP status 416 (Range Not Satisfiable) is returned.

It is also possible to pre-seek for a specific position of the Source to more efficiently deliver range results. To do that, you can provide a function where the pre-seek happens:

public Result index(Http.Request request) {
  String content = "This is the full content!";
  return RangeResults.ofSource(
      request,
      (long) content.length(),
      offset ->
          new RangeResults.SourceAndOffset(offset, sourceFrom(content).drop(offset)),
      "file.txt",
      MimeTypes.TEXT);
}

Next: Session and Flash scopes