Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Github Webhook in Java?

Simple question.

Got registered Payload URL on Github: using ngrok.com (ngrok) link like explained in Github documentation for Webhooks: Creating Webhooks

ngrok definition: “I want to securely expose a local web server to the internet and capture all traffic for detailed inspection and replay.”


When i send POST request with payload from github on correct Payload URL the response code is 200, how can I handle that request/response and get payload (JSON) in java? With servlet or?
I have no idea where to start. Tried to search but nothing for Java :(

  1. If i put ngrok.com/something, Eclipse console throw:
    [WARN] 404 - POST /pas (127.0.0.1) 1368 bytes Request headers Host: ....ngrok.com X-Real-IP: 192.... X-Forwarded-Proto: http Connection: close Content-Length: 5759 Accept: */* User-Agent: GitHub-Hookshot/e9dfd89 X-GitHub-Event: ping X-GitHub-Delivery: c5493000-b67e-11e4-8199-8b09d3d66948 Content-Type: application/json X-Hub-Signature: sha1=b2947ce6a6de23f4274831523bae375d64e20021 Response headers Connection: close Content-Type: text/html;charset=ISO-8859-1 Cache-Control: must-revalidate,no-cache,no-store Content-Length: 1368
  2. If i put good URL, status is 200. Response on Github Webhooks / Manage webhook:
    Accept-Ranges: bytes Connection: keep-alive Content-Length: 1521 Content-Type: text/html Date: Tue, 17 Feb 2015 10:17:46 GMT Last-Modified: Thu, 12 Feb 2015 09:06:18 GMT Server: nginx/1.6.2

So the question is actually "How to handle that payload?"

  • In documentation they use Sinatra and that's a big ? for me.

Sinatra code looks like this:
require "sinatra" require "json" post "/payload" do push = JSON.parse(request.body.read) puts "I got some JSON: #{push.inspect}" end
New to this, sorry if its stupid question.

like image 932
dev_in_progress Avatar asked Nov 15 '25 22:11

dev_in_progress


1 Answers

Resolved, i used HttpServlet doPost method to fetch request, then from request i getReader() and read line so i can make JSONObject. My servlet is on page/Payload and Webhook is on http://server.com/page/Payload

public class Payload extends HttpServlet {

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    StringBuilder builder = new StringBuilder();
    String aux = "";

    while ((aux = req.getReader().readLine()) != null) {
        builder.append(aux);
    }

    String text = builder.toString();
    try {
        JSONObject json = new JSONObject(text);
        String teams_url = json.getJSONObject("repository").getString("teams_url");
        System.out.println("Teams URL:: "+teams_url);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }}
like image 117
dev_in_progress Avatar answered Nov 17 '25 18:11

dev_in_progress



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!