跳转至

获取仓库具体路径下的内容

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

该接口用于获取仓库指定路径下的文件内容或目录条目信息,返回文件类型、大小、内容编码及 SHA 值,并提供对应的 API 地址、网页地址与下载地址;当路径指向目录时,返回该目录下的文件条目数组。

访问令牌支持说明

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

是否允许匿名访问

此接口允许匿名访问

请求参数

参数名 位置 类型 必填 说明
owner path string 是 仓库所属空间地址(组织或个人的地址 path)
repo path string 是 仓库路径(path)
path path string 是 文件的路径
ref query string 否 分支、tag 或 commit;默认:仓库的默认分支(main)

响应参数

字段 类型 必返 可为空 说明
type string 是 否 内容类型,如 file、dir
encoding string 是 否 内容编码方式,如 base64
size integer 是 否 文件大小(字节)
name string 是 否 文件名称
path string 是 否 文件路径
content string 是 是 文件内容(可能为 null)
sha string 是 否 文件 SHA 值
url string 是 否 内容对应的 API 地址
html_url string 是 否 内容对应的网页地址
download_url string 是 否 内容下载地址
_links object 是 否 相关链接信息

_links 对象

字段 类型 必返 可为空 说明
_links.self string 是 否 内容对应的 API 地址
_links.html string 是 否 内容对应的网页地址

Note: 当 path 指向目录时,响应为文件条目数组,每个元素结构与上方单个文件对象一致。

失败响应

以下为接口可能返回的失败响应示例;错误字段名以后端实际返回为准。

400 参数错误

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

401 未认证

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

404 内容不存在

{
  "error": "not_found",
  "message": "仓库或路径不存在"
}

HTTP 响应状态码

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

请求示例

参数说明

  • <owner> — 仓库所属空间地址(组织或个人的地址 path);
  • <repo> — 仓库路径(path);
  • <path> — 要获取内容的文件路径;
  • <ref> — 分支、tag 或 commit;不传时使用仓库默认分支(main)。

本接口允许匿名访问,示例中可不携带访问令牌;如需携带,可追加 ?access_token=<token>。

cURL

curl -X GET 'https://api.atomgit.com/api/v5/repos/<owner>/<repo>/contents/<path>?ref=<ref>' \
  -H 'Accept: application/json'

JavaScript

// 原生 fetch (Node.js 18+ / 浏览器)
const response = await fetch("https://api.atomgit.com/api/v5/repos/<owner>/<repo>/contents/<path>?ref=<ref>", {
  headers: {
    "Accept": "application/json",
  },
});

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

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

Python

import requests

resp = requests.get(
    "https://api.atomgit.com/api/v5/repos/<owner>/<repo>/contents/<path>",
    params={"ref": "<ref>"},
    headers={"Accept": "application/json"},
    timeout=10,
)
resp.raise_for_status()

print(resp.json())

响应示例

状态码: 200

{
  "type": "file",
  "encoding": "base64",
  "size": 19,
  "name": "Note2.md",
  "path": "Note2.md",
  "content": "JXU2RDRCJXU4QkQ1d2ViaG9vaw==",
  "sha": "e5699fe1b360d6c799ee58b24fb5a670b1e14851",
  "url": "https://api.atomgit.com/api/v5/repos/daming_1/zhu_di/contents/Note2.md",
  "html_url": "https://api.atomgit.com/daming_1/zhu_di/blob/master/Note2.md",
  "download_url": "https://api.atomgit.com/daming_1/zhu_di/raw/master/Note2.md",
  "_links": {
    "self": "https://api.atomgit.com/api/v5/repos/daming_1/zhu_di/contents/Note2.md",
    "html": "https://api.atomgit.com/daming_1/zhu_di/blob/master/Note2.md"
  }
}

错误码

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

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