Get the data from request body

Report a typo

Add the code to get the data from the request body and process the request.

public Headers handlePostRequest(HttpExchange exchange) throws IOException {
    String response = "";
    
    try (BufferedReader reader = new BufferedReader(_______)) {
        while(reader.ready()) {
            response += reader.readLine();
        }
    }
    
    response = "Hello, " + response.split(":")[1].replaceAll("[^a-zA-Z]", "");

    exchange.sendResponseHeaders(200, response.length());

    try (OutputStream stream = exchange.getResponseBody()) {
        stream.write(response.getBytes());
    }

    return exchange.getResponseHeaders();
}
Enter a short text
___

Create a free account to access the full topic