Created
October 25, 2022 08:54
-
-
Save todvora/e3b4442e05d6c61589e0a337621c9649 to your computer and use it in GitHub Desktop.
Webhook tester
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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