Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Intro to Java Web Development with Spark Diving into Web Development Cookies

If the username contains a whitespace character such as " ", I get a 500 internal error. Is there any way to fix this?

This is a part of the error message I get:

[qtp694881261-17] ERROR spark.http.matching.GeneralError - 
java.lang.IllegalArgumentException: RFC6265 Cookie values may not contain character: [ ]

Line where the error starts:

at com.teamtreehouse.courses.Main.lambda$main$1(Main.java:25)

Line causing error:

res.cookie("username", username);

Example name: James Roger

1 Answer

You could try converting the space characters to something else, then converting them back to spaces when reading the cookie.

One way to do this would be to import java.net.URLEncoder and java.net.URLDecoder, and changing res.cookie("username", username); to res.cookie("username", URLEncoder.encode(username, "utf-8")); which would store the value of the cookie as James+Roger. To decode a non-null value of the cookie, you could use URLDecoder.decode(req.cookie("username"), "utf-8"), which would give you James Roger.