Search code examples
nginxgitlabgitlab-api

Gitlab REST API file download in subfolder


I was trying out the gitlab REST API on my local server and was having some issues, retrieving files from subfolders. The following requests worked:

curl --header "PRIVATE-TOKEN: <private-token>" "https://my.domain.com/gitlab/api/v4/projects/2/repository/tree?ref=main"
curl --header "PRIVATE-TOKEN: <private-token>" "https://my.domain.com/gitlab/api/v4/projects/2/repository/tree?path=Build&ref=main"
curl --header "PRIVATE-TOKEN: <private-token>" "https://my.domain.com/gitlab/api/v4/projects/2/repository/files/README%2Emd&ref=main"

However when I tried to download a file in a subfolder I always got { error: '404 Not Found' }.

curl --header "PRIVATE-TOKEN: <private-token>" "https://my.domain.com/gitlab/api/v4/projects/2/repository/files/Build%2Ftest%2Etxt&ref=main"

where the folder structure was:

- Build
  - test.txt
- README.md

and my nginx config with gitlab on a relative url, hosted by a docker container, looked like this:

################
## GITLAB DEV ##
################
  location /dev {
    root /home/git/gitlab/public;
    client_max_body_size 0;

    proxy_http_version 1.1;
    proxy_pass http://193.174.29.12:1234/dev;
    # proxy_max_temp_file_size 0;

    gzip off;

    proxy_set_header       Host                     $host;
    proxy_set_header       X-Real-IP                $remote_addr;
    proxy_set_header       X-Forwarded-For          $proxy_add_x_forwarded_for;
    proxy_set_header       X-Forwarded-For-Proto    https;
    proxy_set_header       X-Forwarded-Ssl          on;

  }

Solution

  • The problem here was the nginx config for using gitlab with a relative url. I had to change

    proxy_pass http://193.174.29.12:1234/dev;
    

    to

    proxy_pass http://193.174.29.12:1234;
    

    because everything else seemed to decode the url and the request would change from

    curl --header "PRIVATE-TOKEN: <private-token>" "https://my.domain.com/gitlab/api/v4/projects/2/repository/files/Build%2Ftest%2Etxt&ref=main"
    

    to

    curl --header "PRIVATE-TOKEN: <private-token>" "https://my.domain.com/gitlab/api/v4/projects/2/repository/files/Build/test.txt&ref=main"
    

    and therefore wasn't recogniced by gitlab.