Clean way to parse the incoming request

I have completed 8 stages in the HTTP Server challenge and the way I currently parse the input request is like this.

            String input = in.readLine();
            in.readLine();

            String userAgent = in.readLine();
            in.readLine();
            in.readLine();
            String[] path = in.readLine().split("/");

If I use the below code, it just blocks the execution of the whole program.

        List<String> input = new ArrayList<>();
        String current;
        while ((current = in.readLine()) != null) {
            System.out.println(current);
            input.add(current);
        }

Is there a way I can make my code cleaner/elegant while it still works as expected.

Congratulations on completing the main stages of the HTTP server challenge! :tada:

Clean or elegant code can be a subjective matter. There’s nothing wrong with how you currently parse requests.

One suggestion I can offer is to refactor out a Request class, and let it handle parsing requests and storing the relevant info. This approach keeps the parsing hidden from later steps, thus making the code cleaner in a certain sense

I don’t have an example in Java, but this is how I did it in python:

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.