T
T
TemaKam2021-10-04 12:44:23
Python
TemaKam, 2021-10-04 12:44:23

Why doesn't commit via api work?

trying to commit
https://docs.gitlab.com/ee/api/commits.html#create...

def create_commit(self, project: str, ref: str) -> dict:
        self.auth.update({"Content-Type": "application/json"})
        data = {
                "branch": ref,
                "commit_message": "create",
                "actions": [
                    {"action": "create", "file_path": "1.json", "content": "test"}
                ],
                }
        res = requests.post(
            url=f"{self.base_path}/api/v4/projects/{project}/repository/commits",
            headers=self.auth,
            data=json.dumps(data),
        )
        res.raise_for_status()
        return res.json()

always returns 400 Client Error: Bad Request
at least tried it, what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2021-10-04
@TemaKam

The soremix recommendation is correct. Successful request:

import requests
url = 'https://gitlab.com/api/v4/projects/123/repository/commits'
data = {"branch": "main", "commit_message": "create", "actions": [{"action": "create", "file_path": "1.json", "content": "test"}]}
headers={'PRIVATE-TOKEN': 'token'}

res = requests.post(url, headers=headers, json=data)

An important point - action create returns 400 if the file already exists in the repository

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question