8
votes

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

1
The linked doc mentions that the file path should be URL-encoded. Normally . is URL-safe, but the example encodes it anyway... so did you try with test%2Ejs instead?Thomas
Have you tried http://gitadress/api/v4/projects/id/repository/files/test.js/raw?private_token=PRIVATE_TOKEN&ref=master (with tree changed to files)?Leon

1 Answers

2
votes

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.