How to download single raw file from private gitlab repo using username and password
Asked Answered
I

1

12

I can clone a private repo with username & password using command below

git clone https://some-git-lab-token:[email protected]/foo/bar.git

I'm wondering if it's possible to curl or wget a single raw file from the same repo using the same credential some-git-lab-token:someHash

A sample gitlab raw file url as below

https://gitlab.com/athos.oc/lodash-has-plugin/-/raw/master/.eslintrc.json

I've tried to curl a single file as below but failed

curl https://some-git-lab-token:[email protected]/foo/bar/-/raw/master/testing.js

The result that I got is as below

<html><body>You are being <a href="https://git.xxx.com/users/sign_in">redirected</a>.</body></html>
Interactive answered 9/11, 2021 at 4:16 Comment(1)
This can be achieved with the help of Personal Access token. Can you make use of that?Pandurate
C
17

To download a repository file through curl, you need to use the repository files API endpoint.

Using your example https://gitlab.com/athos.oc/lodash-has-plugin/-/raw/master/.eslintrc.json, would turn into:

https://gitlab.com/api/v4/projects/30349314/repository/files/%2Eeslintrc%2Ejson/raw?ref=master

However, API authentication does not include username and password as an available authentication method, so you would need to use a token (or a session cookie).

Carnify answered 9/11, 2021 at 5:27 Comment(2)
Thanks for the answer! For future reference, you will still need /raw to have the content because without it you are only getting metadata of the file. So the link should be https://gitlab.com/api/v4/projects/30349314/repository/files/%2Eeslintrc%2Ejson/raw?ref=masterInteractive
oh yes, sorry! I copied the wrong example from the docs. I've updated the link and the exampleCarnify

© 2022 - 2024 — McMap. All rights reserved.