【问题标题】:Github API error when creating repo from CL with node, JSON parsing error使用节点从 CL 创建 repo 时出现 Github API 错误,JSON 解析错误
【发布时间】:2014-02-01 16:51:15
【问题描述】:

创建节点 CLI 以从 CL 创建存储库,但在发布到 github api 时出现问题。我正在使用请求模块发布到 github API。

request.post({
    url: 'https://api.github.com/user/repos',
    headers:{
        'User-Agent': 'git CL - node', 
        'Content-type': 'application/json'
    },
    auth:{
        username: '-username-',
        password: '-password-'
    }, 
    form:{ 
        name: "a-new-repo" 
    }

}, function(err, res, body){
    console.log(body);
});

我得到的错误是{"message":"Problems parsing JSON","documentation_url":"http://developer.github.com/v3"}

我尝试了很多东西,比如

  • 设置多部分数据
  • 正文而不是表单数据
  • 设置内容类型
  • 以 JSON 格式发送

我知道的都是对的

  • 身份验证 --- 如果我执行 GET 请求,我能够得到正确的响应,它只是 POST
  • POST 路径和标题

request-module的链接

github-api的链接

【问题讨论】:

    标签: javascript json node.js api github-api


    【解决方案1】:

    json设置为您要发送的数据,而不是form

    request.post({
        url: 'https://api.github.com/user/repos',
        headers:{
            'User-Agent': 'git CL - node', 
            'Content-type': 'application/json'
        },
        auth:{
            username: '-username-',
            password: '-password-'
        }, 
        json:{ 
            name: "a-new-repo" 
        },
    }, function(err, res, body){
        console.log(body);
    });
    

    【讨论】:

      【解决方案2】:

      API 文档中有一个名为json 的参数,您是否尝试将其设置为true

      根据我的阅读,这是将表单数据作为 JSON 结构而不是在请求正文中以表单编码的形式发送所必需的。

      request.post({
          url: 'https://api.github.com/user/repos',
          headers:{
              'User-Agent': 'git CL - node', 
              'Content-type': 'application/json'
          },
          auth:{
              username: '-username-',
              password: '-password-'
          }, 
          form:{ 
              name: "a-new-repo" 
          },
          json: true
      
      }, function(err, res, body){
          console.log(body);
      });
      

      【讨论】:

      • 我得到了答案,不是作为表单发送,而是作为json发送。
      • 您能否提供一个代码为 sn-p 的答案,供其他感兴趣的人使用?
      • @ChrisAlexander 看看其他答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      相关资源
      最近更新 更多