Skip to content

Instantly share code, notes, and snippets.

@todvora
Created October 25, 2022 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todvora/e3b4442e05d6c61589e0a337621c9649 to your computer and use it in GitHub Desktop.
Save todvora/e3b4442e05d6c61589e0a337621c9649 to your computer and use it in GitHub Desktop.
Webhook tester
import com.sun.net.httpserver.HttpServer;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
public class Webhook {
public static final int PORT = 8000;
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(PORT), 0);
System.out.printf("Server listening on http://%s:%d\n", InetAddress.getLocalHost().getHostAddress(), PORT);
server.createContext("/", exchange -> {
System.out.printf("---Req: %s---\n", LocalDateTime.now());
System.out.println(exchange.getRequestMethod() + " " + exchange.getRequestURI());
exchange.getRequestHeaders().forEach((key, value) -> System.out.println(key + ": " + value));
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
if (!body.isBlank()) {
System.out.println("---body---");
System.out.println(body);
}
System.out.println("---end---");
String response = "OK";
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
exchange.close();
});
server.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment