Reputation: 139
According to official documentation curl request is
curl --request POST --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" --form "[email protected]" https://gitlab.example.com/api/v3/projects/5/uploads
I change it to python and get
import requests
headers = {
'PRIVATE-TOKEN': 'my_token'
}
form = {
'file': '@gaa.pdf'
}
print(requests.post('https://gitlab.com/api/v3/projects/4067343/uploads', headers=headers, form=form))
And I have response like
Traceback (most recent call last):
File "/home/k/pro/rat/try.py", line 19, in <module>
print(requests.post('https://gitlab.com/api/v3/projects/4067343/uploads', headers=headers, form=form))
File "/usr/local/lib/python3.5/dist-packages/requests/api.py", line 112, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
TypeError: request() got an unexpected keyword argument 'form'
What should I do to have response with link instead of mistake?
Upvotes: 1
Views: 2644
Reputation: 139
solved
def upload_file(project_id, filename, gitlab_token):
url = 'https://gitlab.com/api/v4/projects/{0}/uploads'.format(project_id)
headers = {'PRIVATE-TOKEN': gitlab_token}
files = {'file': open('{0}'.format(filename), 'rb')}
r = requests.post(url, headers=headers, files=files)
if r.status_code == 200 or r.status_code == 201:
print('Uploading the file {0}....'.format(filename))
else:
print('File {0} was not uploaded'.format(filename))
markdown = r.json()['markdown']
return markdown
Upvotes: 3
Reputation: 1407
You should take a look at Python Gitlab package. It provides you a convenient access to the GitLab server API. The API to upload a file to a project is currently under review in a PR (https://github.com/python-gitlab/python-gitlab/pull/239)
Upvotes: 0