跳转至

新建文件

POST /api/v5/repos/{owner}/{repo}/contents/{path}

该接口用于在指定仓库中新建文件,请求时需提供 base64 编码的文件内容与提交说明,并可选择指定提交分支及提交者信息;创建成功后返回包含 SHA、作者与提交者等信息的提交结果。

访问令牌支持说明

  • AtomGit 用户访问令牌(AtomGit user access tokens)

是否允许匿名访问

此接口不允许匿名访问

请求参数

参数名 位置 类型 必填 说明
owner path string 是 仓库所属空间地址(组织或个人的地址 path)
repo path string 是 仓库路径(path)
path path string 是 文件路径
access_token query string 是 用户授权码

Request Body:

请求体为 JSON 对象:content(必填)为文件内容,需 base64 编码;message(必填)为提交的 commit 信息;branch 与 author 为可选。

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "content": {
      "description": "文件内容,要用 base64 编码",
      "type": "string"
    },
    "message": {
      "description": "提交的 commit 信息",
      "type": "string"
    },
    "branch": {
      "description": "提交的分支",
      "type": "string"
    },
    "author": {
      "description": "提交者信息",
      "type": "object",
      "properties": {
        "name": {
          "description": "Author 的名字,默认为当前用户的名字",
          "type": "string"
        },
        "email": {
          "description": "Author 的邮箱,默认为当前用户的邮箱",
          "type": "string"
        }
      }
    }
  },
  "required": ["content", "message"]
}

响应参数

字段 类型 必返 可为空 说明
commit object 是 否 提交信息

commit 对象

字段 类型 必返 可为空 说明
commit.sha string 是 否 提交的 SHA
commit.author object 是 否 作者信息
commit.committer object 是 否 提交者信息
commit.message string 是 否 提交信息
commit.parents object[] 是 否 父提交列表

commit.author 对象

字段 类型 必返 可为空 说明
commit.author.name string 是 否 作者名字
commit.author.email string 是 否 作者邮箱
commit.author.date string 是 否 作者提交时间

commit.committer 对象

字段 类型 必返 可为空 说明
commit.committer.name string 是 否 提交者名字
commit.committer.email string 是 否 提交者邮箱
commit.committer.date string 是 否 提交时间

commit.parents[] 对象

字段 类型 必返 可为空 说明
commit.parents[].sha string 是 否 父提交的 SHA

失败响应

400 参数错误

{
  "error": "invalid_request",
  "message": "参数 content 不能为空"
}

401 未认证

{
  "error": "unauthorized",
  "message": "缺少或无效的访问令牌"
}

Note: 错误字段名以后端实际返回为准。

HTTP 响应状态码

状态码 说明
200 成功(OK)
400 参数错误(Bad Request)
401 需要认证(Requires authentication)
403 禁止访问(Forbidden)
404 资源不存在(Not Found)
500 系统内部错误(Internal Server Error)

请求示例

参数说明

<owner> 替换为仓库所属空间地址(企业、组织或个人的地址 path); <repo> 替换为仓库路径(path); <path> 替换为要创建的文件路径(如 docs/README.md); <token> 替换为你的 AtomGit 用户访问令牌。

cURL

curl -X POST 'https://api.atomgit.com/api/v5/repos/<owner>/<repo>/contents/<path>?access_token=<token>' \
  -H 'Content-Type: application/json' \
  -d '{
    "content": "IyBBbGwgY29weXJpZ2h0IHJlc2VydmVk",
    "message": "create new file",
    "branch": "main",
    "author": {
      "name": "octocat",
      "email": "octocat@example.com"
    }
  }'

JavaScript

// 原生 fetch (Node.js 18+ / 浏览器)
const response = await fetch(
  "https://api.atomgit.com/api/v5/repos/<owner>/<repo>/contents/<path>?access_token=<token>",
  {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      content: "IyBBbGwgY29weXJpZ2h0IHJlc2VydmVk",
      message: "create new file",
      branch: "main",
      author: { name: "octocat", email: "octocat@example.com" }
    })
  }
);

if (!response.ok) {
  throw new Error(`请求失败: ${response.status}`);
}

const data = await response.json();
console.log(data);

Python

import requests

resp = requests.post(
    "https://api.atomgit.com/api/v5/repos/<owner>/<repo>/contents/<path>",
    params={"access_token": "<token>"},
    json={
        "content": "IyBBbGwgY29weXJpZ2h0IHJlc2VydmVk",
        "message": "create new file",
        "branch": "main",
        "author": {"name": "octocat", "email": "octocat@example.com"}
    },
    timeout=10,
)
resp.raise_for_status()

print(resp.json())

响应示例

状态码: 200

{
  "commit": {
    "sha": "668cb104692b30d537b07f3721df9956d073d343",
    "author": {
      "name": "GitCode2023",
      "email": "13328943+gitcode_admin@user.noreply.gitcode.com",
      "date": "2024-04-11T09:15:20+00:00"
    },
    "committer": {
      "name": "Gitee",
      "email": "noreply@gitcode.com",
      "date": "2024-04-11T09:15:20+00:00"
    },
    "message": "22222",
    "parents": [
      {
        "sha": "0117aa5c6bc8e33d18ad8911afa3cbb54a1faabe"
      }
    ]
  }
}

错误码

错误码 说明
401 未认证或凭证无效(缺少/无效的访问凭证)
400 参数错误或请求无法解析
404 请求路径不存在
405 请求方法不支持
429 请求过于频繁,触发限流
500 系统内部错误

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 gitcode-dev/atomgit-api-doc