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)
3 Likes