DEV Community

The Jucktion
The Jucktion

Posted on • Originally published at jucktion.com on

Accessing your standalone Gitlab Snippets with Curl

GitLab is an open-source collaborative git platform with tons of features. Gitlab offers a broad range of features for its users to make git more collaborative and accessible. It offers free tier for users with personal and hobby projects. Users can create private repositories if they don’t want the code to be public. Similar to GitHub’s gists, GitLab has its snippets. Snippets are short standalone programs that usually server a specific purpose. You can have private snippets as well. Here’s how you access your private snippets remotely without logging into your GitLab account.

Gitlab Snippets

GitLab has documented API access for files in a repository, but it’s missing them for standalone snippets. The documentation has snippets access shown here, but it targets snippets that are already inside a project. It says nothing about snippets not inside a project.

You will need:

  • A GitLab private access token(requires login) to access the file remotely or through your terminal.
  • The ID of your snippet.
  • General understanding of curl

Now you will have to format a curl syntax to make it grab your snippets file. The private access token will verify our request so the file inaccessible to anyone but us.

curl --header “PRIVATE-TOKEN: <private-access-token>” “https://gitlab.com/api/v4/snippets/<snippet-id>/raw” -o main.py
Enter fullscreen mode Exit fullscreen mode

Example:

curl --header “PRIVATE-TOKEN: 2LAX6jyj4f8Zb5fd5Ft4yA” “https://gitlab.com/api/v4/snippets/9999999/raw” -o main.py
Enter fullscreen mode Exit fullscreen mode

The “main.py” is file name you want the output to be pasted to. It can be any name you prefer the file name and extension to be saved. Obviously, it it is a source code file, you’ll have to name it properly, but the idea is that It doesn’t have to reflect the exact filename on your snippet.

Curl Operation

If all goes well, you will see a file download progress and the file should appear on your folder. Notice the “Total” column, it should have a file size information. It’s a sign that the command worked.

Top comments (1)

Collapse
 
zoekim profile image
ZoeKim

This is such an interesting choice. Dua to convince in laws for love marriage