GITLAB API在存储库中创建新文件



所以我试图通过我在react代码库中公开的这个api在我的git repo中创建一个新文件,但我得到了403错误?让我知道我哪里错了。

var bodyData = {
token: "**********",
ref: "main",
branch: "demo",
commit_message: "create a new file",
content: "some content"
};
fetch("https://gitlab.com/api/v4/projects/27622003/repository/files/myfile1%2Etxt", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(bodyData),
})
.then((response) => response.json())
.then((data) => {
console.log("response:", data);
})
.catch((error) => {
console.error("Error:", error);
});

您似乎缺少包含要执行的操作类型(创建、移动、更新和删除(的操作数组。

GitLab提交API:https://docs.gitlab.com/ee/api/commits.html

在您的情况下,有效载荷应该如下所示:

var bodyData = {
token: "**********",
ref: "main",
branch: "demo",
commit_message: "create a new file",
actions: [
{
"action": "create",
"file_path": "your/file/path",
"content": "some content"
}
]
};

最新更新