检索自述文件的 HTML

Retrieve HTML of ReadMe

本文关键字:HTML 自述文件 检索      更新时间:2023-09-26

嗨,第一次使用这样的 API。无论如何,我一直在阅读 GitHub API 并遇到了这个:

自述文件支持用于检索原始内容或呈现的 HTML 的自定义媒体类型。

来源:https://developer.github.com/v3/repos/contents/#get-the-readme

我相信这意味着可以检索自述文件内容的 HTML 格式版本?如果是这样,我将如何使用 AJAX 检索它,因为教程都是针对 curl 的。最后,我想在我的网站上显示其中的一部分,如果以 html 格式而不是 markdown 给出会容易得多。

文档说了一些事情: application/vnd.github.VERSION.html

我只是不一定知道如何使用它。

谢谢!

您必须将

HTTP 请求的 Accept 标头设置为 application/vnd.github.html

$.ajax({
  url: 'https://api.github.com/repos/just95/toml.dart/readme',
  headers: { 'Accept': 'application/vnd.github.html' }
}).done(function(data) {
  alert(data);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

需要做的就是设置 HTTPS 请求的 Accept 标头。例如,使用 cURL:

curl -i -H "Accept: application/vnd.github.v3.html" https://api.github.com/repos/github/developer.github.com/readme

在 JavaScript 中,

var apiRoot = 'https://api.github.com';
var myUser = YOUR_USER_HERE;
var myRepo = YOUR_REPO_HERE;
var request = new XMLHttpRequest();
request.open('GET', apiRoot + '/repos/' + myUser + '/' + myRepo + '/readme');
request.setRequestHeader('Accept','application/vnd.github.v3.html');
/* add event listeners... */
request.onreadystatechange = function() {
  if (request.readyState === 4 && request.status === 200) {
    document.body.innerHTML = request.response;
  }
};
request.send();