dev_in_progress
dev_in_progress

Reputation: 2494

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?"

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.

Upvotes: 4

Views: 8652

Answers (2)

Lubo
Lubo

Reputation: 1827

You can make use of gitlab4j-api library. For example usage, have a look at simple review project, exactly here: https://github.com/gitlab4j/simple-cr/blob/master/src/main/java/org/gitlab4j/simplecr/service/GitLabWebHookService.java

Upvotes: 0

dev_in_progress
dev_in_progress

Reputation: 2494

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();
    }
    }}

Upvotes: 3

Related Questions