1

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?

5
  • You forgot to mention what problem you are facing. Commented Sep 7, 2017 at 9:43
  • Added, thank you)))) Commented Sep 7, 2017 at 9:49
  • Is the Token in the wget command truncated or is it your real token ? Commented Sep 7, 2017 at 9:54
  • that is fake one from documents, but I use my token Commented Sep 7, 2017 at 10:00
  • requests.post does not support formparameter. See this question how to convert curl's --form to Python's requests. Commented Sep 7, 2017 at 10:21

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.