Setting cookie in response

return ok(auth.render()).as("text/html")
                .withCookies(Http.Cookie.builder("name", username).withMaxAge(Duration.ofSeconds(900)).build());

Why am I getting a 500 internal error on this line. How do we set the cookie in play framework 2.7. I get below error-

[error] a.a.ActorSystemImpl - Internal server error, sending 500 response
akka.http.impl.util.One2OneBidiFlow$OutputTruncationException: Inner flow was completed without producing result elements for 1 outstanding elements
    at akka.http.impl.util.One2OneBidiFlow$OutputTruncationException$.apply(One2OneBidiFlow.scala:22)
    at akka.http.impl.util.One2OneBidiFlow$OutputTruncationException$.apply(One2OneBidiFlow.scala:22)
    at akka.http.impl.util.One2OneBidiFlow$One2OneBidi$anon$1$anon$4.onUpstreamFinish(One2OneBidiFlow.scala:97)
    at akka.stream.impl.fusing.GraphInterpreter.processEvent(GraphInterpreter.scala:504)
    at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:378)
    at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:588)
    at akka.stream.impl.fusing.GraphInterpreterShell$AsyncInput.execute(ActorGraphInterpreter.scala:472)
    at akka.stream.impl.fusing.GraphInterpreterShell.processEvent(ActorGraphInterpreter.scala:563)
    at akka.stream.impl.fusing.ActorGraphInterpreter.akka$stream$impl$fusing$ActorGraphInterpreter$processEvent(ActorGraphInterpreter.scala:745)
    at akka.stream.impl.fusing.ActorGraphInterpreter$anonfun$receive$1.applyOrElse(ActorGraphInterpreter.scala:760)

If you remove withCookies(...) there is no exception?
Just

return ok(auth.render()).as("text/html");

?

Yes . No issues when I run this. Only on adding cookie I am getting the error

I guess your username variable contains characters which Play by default considers to be invalid when used in a cookie value:

(Here are the values for the cookie name btw:)

You have two options:

  1. Make sure your username variable does not contain invalid characters.

  2. In your application.conf set

play.http.cookies.strict = false

However, for the second option please read and be aware of this comment:

One more thing:
From the exception you posted it’s, of course, hard to tell what the problem is, because akka-http somehow swallows the underlying exception…
As a “workaround”, to see what’s going on, you can switch to the netty backend, which provides you the causing exception (here for example I used the invalid space character in the cookie value):

java.lang.IllegalArgumentException: Cookie value contains an invalid char:  
	at play.core.cookie.encoding.CookieEncoder.validateCookie(CookieEncoder.java:46)
	at play.core.cookie.encoding.ServerCookieEncoder.encode(ServerCookieEncoder.java:71)
	at play.api.mvc.CookieHeaderEncoding.$anonfun$encodeSetCookieHeader$1(Cookie.scala:249)
	at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:237)
	at scala.collection.immutable.List.foreach(List.scala:392)
	at scala.collection.TraversableLike.map(TraversableLike.scala:237)
	at scala.collection.TraversableLike.map$(TraversableLike.scala:230)
	at scala.collection.immutable.List.map(List.scala:298)
	at play.api.mvc.CookieHeaderEncoding.encodeSetCookieHeader(Cookie.scala:240)
	at play.api.mvc.CookieHeaderEncoding.encodeSetCookieHeader$(Cookie.scala:238)

This was it . Thanks a lot