I am trying to read the files and each file's raw contents under the particular folder of the GitHub URL but I am not getting the right response.
Basically, I would like to do something like this:
I have a public GitHub project within which I have a folder
folder1
. Thisfolder1
contains many files eitherXML/JSON
.I want to use the link which is pointing towards
folder1
:https://github.com/comapny/project/tree/master/folder1/tempFolder
and get its content.Under this folder I have many files whose contents I want to read raw data.
As of now, I am able to make requests to single files and their raw data under the folder but unable to read files under the folder and read all files and their content.
Following is the code I have so far:
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class TestMain {
public static void main(String[] args) throws IOException {
//Read the GitHub files
final String inputURL = "https://github.com/company/project/tree/master/XML/withData";
//final String inputURL = "https://raw.githubusercontent.com/company/project/blob/master/XML/withData/myFileName.xml";
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(new HttpGet(inputURL));
StatusLine statusLine = response.getStatusLine();
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
System.out.println("Response Code : " statusLine.getStatusCode() " ---- " "Response Phrase : " statusLine.getReasonPhrase());
System.out.println("Response body: " responseBody);
}
}
CodePudding user response:
You can use GitHub's repository content API to list files in a directory as well as read file contents.
For example, listing the files in the directory called "XML" could look like the following:
https://api.github.com/repos/company/project/contents/XML/
The response would be an array of items in the directory. Each item includes the property type
, which lets you know whether the item is a file or directory, as well as url
, a link to request that item.
By performing a new request to an item's URL, you can access the contents of a file through its content
property, or if it is a directory, you can access another array of files inside.