How to get file path? How to fetch a file?

I cannot get a file from our gitlab repository.

From the docs it follows that paths should be like:

curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/13083/repository/files/app%2Fmodels%2Fkey%2Erb/raw?ref=master"

but in the GitLab UI I couldn’t manage to find ANYTHING like that.
The best I could find so far looks like this:

https://gitlab.example.com/group/group/myproject/-/raw/develop/file.yaml

I searched carefully in the UI but there is no API links or paths to a file.
How do you manage to use it?

Hi,

is the project ID which you need to pass into the API request URL. That can be seen in the project overview in the UI, or by searching/getting the project via API.

For the API request itself, the endpoint is constructed with /api/v4/projects/13083/repository/files/ and the file structure underneath.

If I navigate to a specific file, https://gitlab.com/everyonecancontribute/web/everyonecancontribute.gitlab.io/-/blob/main/config.toml I know that /blob/main is the reference to the current branch main and everything after it represents the relative file path.

It is manual work though, and I try to avoid constructing URLs without first having searched and matched a pattern, e.g. fetching all files with the *.toml or *.md file suffix.

  1. Repository tree search
  2. Use file_path in the URL to fetch the raw content

I’m a fan of more programmatic approach with abstracting the API client to e.g. python-gitlab. I’ve created a quick example code snippet.

Works pretty good, never did that myself before.

Cheers,
Michael

2 Likes