Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.