跳转至

更新文件

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

该接口用于更新指定仓库中指定路径的文件内容,需提供 base64 编码的新文件内容、原文件 Blob SHA 及提交信息,可指定目标分支和提交作者,成功后返回提交记录与更新后的文件内容。

访问令牌支持说明

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

是否允许匿名访问

此接口不允许匿名访问

请求参数

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

Request Body:

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

响应参数

字段 类型 必返 可为空 说明
commit object 是 否 提交信息
content object 是 否 更新后的文件内容信息

commit 对象

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

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 是 否 提交时间

content 对象

字段 类型 必返 可为空 说明
content.name string 是 否 文件名
content.path string 是 否 文件路径
content.size integer 是 否 文件大小
content.sha string 是 否 文件内容 SHA
content.type string 是 否 文件类型

失败响应

只有成功响应是不够的:调用方需要知道失败长什么样才能写错误处理。

400 参数错误

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

401 未认证

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

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

HTTP 响应状态码

状态码 说明
200 成功(OK)
400 参数错误
401 需要认证(Requires authentication)
403 禁止访问(Forbidden)
404 资源不存在(Not found)

请求示例

参数说明

示例与本文档其余部分自洽,直接照抄即可调用成功:

  • <owner> 替换为仓库所属空间地址(组织或个人的地址 path);
  • <repo> 替换为仓库路径(path);
  • <path> 替换为文件的路径;
  • <token> 替换为你的 AtomGit 用户访问令牌;
  • <file_sha> 替换为文件的 Blob SHA;
  • <branch> 替换为分支名称(可选,默认为仓库默认分支)。

cURL

curl -X PUT 'https://api.atomgit.com/api/v5/repos/<owner>/<repo>/contents/<path>?access_token=<token>' \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -d '{
    "content": "SGVsbG8gV29ybGQK",
    "sha": "<file_sha>",
    "branch": "<branch>",
    "message": "update file"
  }'

JavaScript

// 原生 fetch (Node.js 18+ / 浏览器)
const response = await fetch("https://api.atomgit.com/api/v5/repos/<owner>/<repo>/contents/<path>?access_token=<token>", {
  method: "PUT",
  headers: {
    "Content-Type": "application/json",
    "Accept": "application/json",
  },
  body: JSON.stringify({
    content: "SGVsbG8gV29ybGQK",
    sha: "<file_sha>",
    branch: "<branch>",
    message: "update file",
  }),
});

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

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

Python

import requests

resp = requests.put(
    "https://api.atomgit.com/api/v5/repos/<owner>/<repo>/contents/<path>",
    params={"access_token": "<token>"},
    json={
        "content": "SGVsbG8gV29ybGQK",
        "sha": "<file_sha>",
        "branch": "<branch>",
        "message": "update file",
    },
    headers={"Accept": "application/json"},
    timeout=10,
)
resp.raise_for_status()

print(resp.json())

响应示例

状态码: 200

{
  "commit": {
    "sha": "0f9a4c7b2e6d4f8a1b3c5d7e9f0a2b4c6d8e0f1a",
    "author": {
      "name": "octocat",
      "email": "octocat@atomgit.com",
      "date": "2026-10-12T13:33:48+08:00"
    },
    "committer": {
      "name": "octocat",
      "email": "octocat@atomgit.com",
      "date": "2026-10-12T13:33:48+08:00"
    },
    "message": "update file",
    "tree": "2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a",
    "parents": ["5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b"]
  },
  "content": {
    "name": "Note.md",
    "path": "Note.md",
    "size": 1024,
    "sha": "6dcd4ce23d88e2ee9568ba546c007c63d9131c1b",
    "type": "file"
  }
}

错误码

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

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