Skip to content

Can't save file content #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
araichev opened this issue Oct 27, 2016 · 2 comments
Closed

Can't save file content #175

araichev opened this issue Oct 27, 2016 · 2 comments

Comments

@araichev
Copy link

araichev commented Oct 27, 2016

Hi there,

Using Python 3.5 and python-gitlab 0.16, i can't save some file content to Gitlab.
More specifically, running this code

from pathlib import Path

path = Path('..')/'test.txt'  # Text file with contents "Goodbye, world"
project_id = 'secret_ID'
file_path = 'data/' + path.name
f = gl.project_files.get(file_path=file_path, ref='master', project_id=project_id)   
f.content = base64.b64encode(path.open('rb').read())
f.save(branch_name='master', commit_message='Updated ' + file_path , encoding='base64')

i get this error

TypeError                                 Traceback (most recent call last)
<ipython-input-17-7015cf3aaaad> in <module>()
     12 
     13 f.content = base64.b64encode(path.open('rb').read())
---> 14 f.save(branch_name='master', commit_message='Updated ' + file_path , encoding='base64')

/home/hidden_user/.virtualenvs/hidden_dir/lib/python3.5/site-packages/gitlab/objects.py in save(self, **kwargs)
    318     def save(self, **kwargs):
    319         if self._from_api:
--> 320             self._update(**kwargs)
    321         else:
    322             self._create(**kwargs)

/home/hidden_user/.virtualenvs/hidden_dir/lib/python3.5/site-packages/gitlab/objects.py in _update(self, **kwargs)
    313             raise NotImplementedError
    314 
--> 315         json = self.gitlab.update(self, **kwargs)
    316         self._set_from_dict(json)
    317 

/home/hidden_user/.virtualenvs/hidden_dir/lib/python3.5/site-packages/gitlab/__init__.py in update(self, obj, **kwargs)
    621 
    622         # build data that can really be sent to server
--> 623         data = obj._data_for_gitlab(extra_parameters=kwargs, update=True)
    624 
    625         r = self._raw_put(url, data=data, content_type='application/json')

/home/hidden_user/.virtualenvs/hidden_dir/lib/python3.5/site-packages/gitlab/objects.py in _data_for_gitlab(self, extra_parameters, update, as_json)
    227         data.update(extra_parameters)
    228 
--> 229         return json.dumps(data) if as_json else data
    230 
    231     @classmethod

/usr/lib/python3.5/json/__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    228         cls is None and indent is None and separators is None and
    229         default is None and not sort_keys and not kw):
--> 230         return _default_encoder.encode(obj)
    231     if cls is None:
    232         cls = JSONEncoder

/usr/lib/python3.5/json/encoder.py in encode(self, o)
    196         # exceptions aren't as detailed.  The list call should be roughly
    197         # equivalent to the PySequence_Fast that ''.join() would do.
--> 198         chunks = self.iterencode(o, _one_shot=True)
    199         if not isinstance(chunks, (list, tuple)):
    200             chunks = list(chunks)

/usr/lib/python3.5/json/encoder.py in iterencode(self, o, _one_shot)
    254                 self.key_separator, self.item_separator, self.sort_keys,
    255                 self.skipkeys, _one_shot)
--> 256         return _iterencode(o, 0)
    257 
    258 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

/usr/lib/python3.5/json/encoder.py in default(self, o)
    177 
    178         """
--> 179         raise TypeError(repr(o) + " is not JSON serializable")
    180 
    181     def encode(self, o):

TypeError: b'R29vZGJ5ZSwgd29ybGQK' is not JSON serializable
@gpocentek
Copy link
Contributor

The content attribute must be a string, not a byte array. I've updated the docs to reflect this restriction.

@araichev
Copy link
Author

araichev commented Nov 16, 2016

Thanks.

Related to that, how would one post a zip file to a Gitlab repository using python-gitlab?

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants