rest - GitHub API 返回错误的文件内容

标签 rest github github-api

我正在尝试使用 GitHub API 创建文件。文件应包含单个字节0xDD。我正在使用以下命令:

http PUT https://api.github.com/repos/koluch/test/contents/data.txt "message"="New file" "content"="3Q==" Authorization:"token <OAUTH_TOKEN>"

3Q==0xDD 字节的 Base64 表示。该请求返回200;但是,当我尝试使用 GET 请求检索文件时...

http GET https://api.github.com/repos/koluch/test/contents/data.txt 

...它返回以下 JSON:

{
    "_links": {
        "git": "https://api.github.com/repos/koluch/test/git/blobs/4e3bc519eef7d5a35fff67687eaee65616832e45",
        "html": "https://github.com/koluch/test/blob/master/data.txt",
        "self": "https://api.github.com/repos/koluch/test/contents/data.txt?ref=master"
    },
    "content": "77+9\n",
    "download_url": "https://raw.githubusercontent.com/koluch/test/master/data.txt",
    "encoding": "base64",
    "git_url": "https://api.github.com/repos/koluch/test/git/blobs/4e3bc519eef7d5a35fff67687eaee65616832e45",
    "html_url": "https://github.com/koluch/test/blob/master/data.txt",
    "name": "data.txt",
    "path": "data.txt",
    "sha": "4e3bc519eef7d5a35fff67687eaee65616832e45",
    "size": 1,
    "type": "file",
    "url": "https://api.github.com/repos/koluch/test/contents/data.txt?ref=master"
}

字段content包含77+9\n值,这不是我的0xDD字节。当我使用下载网址时,一切都很好。

有谁知道这是怎么回事吗?

最佳答案

我仍在了解 github 支持的情况。到目前为止还没有任何进展。但我想我知道问题所在。看起来 github 试图猜测文件的文件类型,如果它认为它是文本文件,则使用检测到的编码来创建缓冲区,然后再将其编码为 base64。

这至少是我从原始请求的响应 header 中看到的。我的解决方法是检查文件大小,如果不匹配,则使用原始 api 再次获取 blob:

      const res = await octokit.repos.getContents(params);
      let data = Buffer.from(res.data.content, 'base64');
      if (data.length !== res.data.size) {
        log.debug(`base64 decoded content length doesn't match: ${data.length} != ${res.data.size}`);
        log.debug(`fetching content from ${res.data.download_url}`);
        const result = await rp.get({
          url: res.data.download_url,
          resolveWithFullResponse: true,
          encoding: null,
        });
        data = result.body;
      }

关于rest - GitHub API 返回错误的文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36519095/

相关文章:

graphql - 如何查询 GitHub v4 API 以获取特定标签处的目录内容?

java - Android 和 AppEngine 网络服务 : Json. .. RPC、REST... Protocol Buffer ?

rest - 在 Backbone 中使用超媒体 (REST) API

git - 如何通过 plink 获得对 github 的读写访问权限?

macos - 无法将分支推送到 github "Use git@github.com:Paratron/spacebattles.git"

r - httr github-API 回调 URL 问题

android - 带有 Android 的 Grape Rails API

java - 带分页的 API 的 Stream 与 Iterator

Git merge : What do the pluses mean?

Github V3 API : list contributors