How to commit a image via gitlab commit API?

I want to create a commit of updating a image file in my repository. How to do it via gitlab API?

Hey @zxlc

Did you check the GitLab API documentation https://docs.gitlab.com/ee/api/commits.html#create-a-commit-with-multiple-files-and-actions ? Are you having any specific issue to use this endpoint?

Does commit API supports sending binary data? Is there any sample code?

data = {
        'branch': 'master',
        'commit_message': 'some message',
        'actions': [{
            'action': 'update',
            'file_path': 'pic1.jpeg',
            'content': imageData
        }]
    }

resp = requests.post(
        url='http://gitlab.wzx.com/api/v4/projects/1/repository/commits',
        data=data,
        headers={
            "PRIVATE-TOKEN": PRIVATE_TOKEN
        }
    )

imageData is type of bytes, when I send this request, I received the following response:

{
    message: "500 Internal Server Error"
}

As stated in Gitlab doc, you’ll have to encode data in base64 and add encoding: "base64" to actions parameter.

Example using node-gitlab:

			commitActions = [{
				action: "create",
				file_path: filename,
				content: fs.readFileSync(file.path).toString("base64"),
				encoding: "base64"
			}]
            const response = await api.Commits.create(project.id, "master", "Initialize datapackage", commitActions)
2 Likes

Does it work with the graphQL API ?

I can commit any text files via the graphQL API. But when I commit an image or some base64 encoded data, it doesn’t show the image, only the raw base64.

Note : when adding encoding: "base64" to a commit action, gitlab returns an error saying that I should use BASE64. So I uppercase it and I can commit via the graphQL API. But gitlab doesn’t seems to recognize the file as an image. I got the raw string as a base64. (Something like iVBORw0KGgoAAAANSUhEUgAABBAAAAQRCAMAAACn54tnAAAAA3NCSVQICAjb4U/gAAAAIVBMVEUjHyD///+9u7zq6ene3t…)

I tried with and without prefixing the content by data:image/jpg;base64,.
Thanks for your response!