【问题标题】:github API unsupported media type 415github API 不支持的媒体类型 415
【发布时间】:2021-10-21 17:07:30
【问题描述】:

我在 Meteor 中使用 github API 但无法解决这个问题:

此代码尝试获取某个 repo 的总流量。

HTTP.call( 'GET', 'https://api.github.com/repos/hackmdio/hackmd/traffic/views', 
    {
        headers: 
        {
            'Content-Type':'application/json',
            "Accept":"application/vnd.github.v3+json",
            "User-Agent": "whales"
        },
    },
    function( error, response ) {
  if ( error ) {
    console.log('---------------------------error occurred-----------------------------------')
    console.log('---------------------------error occurred-----------------------------------')

    console.log( error );
  } else {

    console.log('--------------------------data got it!!-------------------------------------')
    console.log('--------------------------data got it!!-------------------------------------')

    console.log(response);
  }
});

错误:

{
  "message": "If you would like to help us test the Repo Traffic API during its preview period, you must specify a custom media type in the 'Accept' header. Please see the docs for full details.",
  "documentation_url": "https://developer.github.com/v3"
}

我搜索了类似的问题并添加了“Content-Type”和“Accept”,但它仍然无法正常工作。

然后我尝试在 Postman 和终端中使用相同的标题执行此操作,但此错误一直在发生。

非常感谢。

【问题讨论】:

    标签: api http meteor github github-api


    【解决方案1】:

    您需要在请求中添加一个Accept: application/vnd.github.spiderman-preview 标头,以便在 Repo Traffic API 处于预览状态时访问它。来自API docs

    存储库流量的 API 目前可供开发人员预览。在预览期间,API 可能会更改,恕不另行通知。详情请参阅博文。

    要访问 API,您必须在 Accept 标头中提供自定义媒体类型:

    application/vnd.github.spiderman-preview

    【讨论】:

    • 他们明确告诉你这样做......但很容易忘记
    • 此外,如果有人使用仍在开发人员预览中的任何其他功能,最好使用此概览页面 docs.github.com/en/rest/overview/api-previews 选择正确的值 - 我正在使用项目,我必须选择 application/vnd.github.inertia-preview .
    【解决方案2】:

    提交搜索 API 目前可供开发人员预览。在预览期间,API 可能会更改,恕不另行通知。

    要访问 API,您必须在 Accept 标头中提供自定义媒体类型:

     Accept: application/vnd.github.cloak-preview
    

    ☝️此标头是必需的。

    check docs

    【讨论】:

      【解决方案3】:

      致在谷歌上搜索为什么 GitLab API 在发送 POST 请求时开始抛出 415 后最终进入此页面的任何人:

      如果您通过 post-data 字段发送内容,请确保传递 Content-Type: application/x-www-form-urlencoded 标头。他们的文档从未提及此 b/c,显然许多客户(如 curl)会自动执行此操作。

      【讨论】:

        猜你喜欢
        • 2021-04-27
        • 2020-10-06
        • 2014-05-10
        • 2019-05-01
        • 2017-09-14
        • 2021-07-20
        • 1970-01-01
        相关资源
        最近更新 更多