0

I have a GitLab API (v4) that I need to call to get a project sub-directory (something apparently new in v.14.4, it seems not yet included python-gitlab libs), which in curl can be done with the following command:

curl --header "PRIVATE-TOKEN: A_Token001" http://192.168.156.55/api/v4/projects/10/repository/archive?path=ProjectSubDirectory --output ~./temp/ProjectSubDirectory.tar.gz

The issue is in the last part, the --output ~./GitLab/some_project_files/ProjectSubDirectory.tar.gz

I tried different methods (.content, .text) which failed, as:

...
response = requests.get(url=url, headers=headers, params=params).content
# and save the respon content with with open(...)

but in all the cases it saved a non-valid tar.gz file, or other issues.

I even tried https://curlconverter.com/, but the code it generates does not work as well, it seems ignoring precisely the --output parameter, not showing anything about the file itself:

headers = {'PRIVATE-TOKEN': 'A_Token001',}
params = (('path', 'ProjectSubDirectory'),)
response = requests.get('http://192.168.156.55/api/v4/projects/10/repository/archive', headers=headers, params=params)

For now, I just created a script and call it with sub-process, but I don't like much this approach due to Python has libraries, as requests, that I guess should have some way to do the same...

xCovelus
  • 578
  • 1
  • 9
  • 20
  • How exactly did you use `open`? By default, it opens a file in text (not binary), mode, which means your Gzip file is going to be subjected to a lot of undesirable encoding as it is written. – chepner Dec 17 '21 at 18:30
  • response.contet should give you the binary data https://docs.python-requests.org/en/latest/user/quickstart/#binary-response-content – Gonzalo Odiard Dec 17 '21 at 18:36

1 Answers1

0

2 key things.

  1. Allow redirects
  2. Use raise_for_status() to make sure the request was successful before writing the file. This will help uncover other potential issues, like failed authentication.

After that write response.content to a file opened in binary mode for writing ('wb')

import requests
url = "https://..."
headers = {} # ...
paramus = {} # ...
output_path = 'path/to/local/file.tar.gz'
response = requests.get(url, headers=headers, params=params, allow_redirects=True)
response.raise_for_status() # make sure the request is successful
with open(output_path, 'wb') as f:
    f.write(response.content)
sytech
  • 29,298
  • 3
  • 45
  • 86