Search code examples
python-2.7gitlabgitlab-api

Http 404 when trying to get files with special characters from GitLab API


I am using python-gitlab driver for the Gitlab API. The following work fine on my repository:

>>> rf_blob = p.repository_blob(p_latest_commit.id, 'iOSBoilerplate/GooglePlus/GoogleOpenSource.framework/Versions/A/Headers/GTMOAuth2Authentication.h')
>>> rf_blob = p.repository_blob(p_latest_commit.id, 'iOSBoilerplate/GooglePlus/GoogleOpenSource.framework/Versions/A/Headers/GTMOAuth2SignIn.h')

However, when I try to open a file that contains a + character in it, I get the following even though that file clearly exists in the repository:

>>> rf_blob = p.repository_blob(p_latest_commit.id, 'iOSBoilerplate/GooglePlus/GoogleOpenSource.framework/Versions/A/Headers/GTMNSString+URLArguments.h')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/gitlab/objects.py", line 1521, in repository_blob
    raise_error_from_response(r, GitlabGetError)
  File "/usr/local/lib/python2.7/site-packages/gitlab/exceptions.py", line 140, in raise_error_from_response
    response_body=response.content)
gitlab.exceptions.GitlabGetError: 404: 404 File Not Found

How exactly do I go about escaping the + in python so that I can get the blob of the file?


Solution

  • The solution is to use urllib.quote.

    python-gitlab is a superficial wrapper upon the real Gitlab API. The Gitlab API functions on url requests so this solution seemed natural:

    rf_blob = p.repository_blob(p_latest_commit.id, urllib.quote('iOSBoilerplate/GooglePlus/GoogleOpenSource.framework/Versions/A/Headers/GTMNSString+URLArguments.h'))