Ryan
Ryan

Reputation: 2886

Use Java to get Github repositories

I'm using the following code to send a http request to github.

String url = "https://api.github.com/repositories";
try {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost request = new HttpPost(url);
    // StringEntity params = new StringEntity(body);
    request.addHeader("content-type", "application/json");
    // request.setEntity(params);
    HttpResponse result = httpClient.execute(request);
    String json = EntityUtils.toString(result.getEntity(), "UTF-8");

    System.out.println(json);
} catch (IOException ex) {
}

I got output: {"message":"Not Found","documentation_url":"https://developer.github.com/v3"}

If use directly put "https://api.github.com/repositories" in browser, a lot of useful information will be shown. My question is how can I get the information I see when using browser by using Java.

Upvotes: 1

Views: 1754

Answers (1)

alampada
alampada

Reputation: 2409

You should use HttpGet instead of HttpPost. Just like your browser sends a GET request.

Upvotes: 3

Related Questions