I want to get a file content in gitlab by using its api. Firstly, I check my gitlab version, its written as, GitLab Community Edition 9.4.3 b125d21 update asap
Then I create a private token and http://gitadress/api/v4/projects/id/repository/files?private_token=PRIVATE_TOKEN returns as;
{"error":"404 Not Found"}
then I modify the query as;
http://gitadress/api/v4/projects/222/repository/tree?private_token=PRIVATE_TOKEN this request returns as;
[{"id":"8078365d80c","name":"test.js","type":"blob","path":"test.js","mode":"100644"}]
What I want is to get the content of the test.js but whatever I tried I couldn't achieve it.
http://gitadress/api/v4/projects/id/repository/tree/test.js/raw?private_token=PRIVATE_TOKEN&ref=master returns as;
{"error":"404 Not Found"}
How can I get the raw file content by using gitlab api? Documentation is here; https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/repository_files.md#get-raw-file-from-repository
The Repos API allows to create, manage and control the workflow of public and private GitHub repositories.
On GitHub, navigate to the main page of the repository. Under the repository name, click Clone or download. In the Clone with HTTPs section, click to copy the clone URL for the repository. Open Git Bash.
Read a file from Gitlab Using V4 API Let's create a TestController. php file in Http/Controller folder. First, we'll create a connect method that'll connect laravel application with GitLab v4 API. Let's create an endpoint that ll use to read files from GitLab.
The documentation mentions:
Url encoded full path to new file. Ex.
lib%2Fclass%2Erb
That means you need to URL encode test.js
: test%2Ejs
.
(See Percent encoding: character data)
http://gitadress/api/v4/projects/id/repository/files/test%2Ejs/raw?private_token=PRIVATE_TOKEN
You can add ?ref=master
to make sure to get the content from the master
branch for instance.
http://gitadress/api/v4/projects/id/repository/files/test%2Ejs/raw?ref=master&private_token=PRIVATE_TOKEN
That being said, you have the gitlab-ce issue 31470 which is still open:
API to "Get raw file from repository" fails for files with dots
A fix is in progress: gitlab-ce merge_request 13370, and will be delivered for GitLab 9.5.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With