skip to Main Content

Is it possible to download a file from GitLab using the API? I am using CentOS 6 commandline. The documentation for the API says “Get file from repository” but it is only to get the metadata and not the file itself. The example they give is:

curl --request GET --header 'PRIVATE-TOKEN: <your_access_token>' 'https://gitlab.example.com/api/v4/projects/13083/repository/files/test%2Epy/raw?ref=master'

If I use the raw option, it gives me the contents of the file, but it saves the name with as test%2Epy/raw?ref=master

How do I get it to save as test.py?

2

Answers


  1. Append > test.py to curl as below:

    curl --request GET --header 'PRIVATE-TOKEN: ' 'https://gitlab.example.com/api/v4/projects/13083/repository/files/test%2Epy/raw?ref=master' > test.py
    
    Login or Signup to reply.
  2. It’s also possible to use the group and project name instead of the project-id:

      response=$(curl "$GITLAB_URL/api/v4/projects/<group>%2F<project>/repository/files/<folder>%2Ftest%2E.py/raw?ref=master" 
        --silent 
        -o "test.py" 
        --header "PRIVATE-TOKEN: $GITLAB_TOKEN")
    
      if  [[ $response == 4* ]] || [[ $response == 5* ]]; then
        echo ERROR - Http status: "$response"
        exit 1
      fi
    
    

    It’s important to URL encode the group + project path and the file path as well.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search