我们正在尝试使用nodejs https
库向circleci rest api发出api请求,并获得看似JSON格式错误的内容。不幸的是,这只发生在我们使用节点而不是,例如, curl
,或邮递员。
这是节点代码
var https = require('https');
https.get("https://circleci.com/api/v1/projects?circle-token=OUR-API-KEY", function(response) {
var body = "";
response.on('data', function (chunk) {
body += chunk;
});
response.on('end', function () {
console.log("TEST", body);
});
});
这是结果的第一部分
TEST ({:irc_server nil, :scopes (:write-settings :view-builds :read-settings :trigger-builds :all :status :none), :irc_keyword nil,
以下是我们使用curl
$ curl https://circleci.com/api/v1/projects?circle-token=OUR-API-KEY
[ {
"irc_server" : null,
"ssh_keys" : [ ],
"branches" : {
"master" : {
"last_non_success" : {
"outcome" : "failed",
"status" : "failed",
"build_num" : 41,
...
我们还注意到java
响应中似乎有一些node
个残留。以下是节点响应的不同部分的示例
...
"pushed_at" #object[org.joda.time.DateTime 0x2753b900 "2015-11-29T14:22:42.000Z"],
"added_at" #object[org.joda.time.DateTime 0x686553cb "2015-11-30T16:07:28.250Z"]},
...
据我们所知,这不是节点问题,因为当我们将脚本中的地址更改为“https://randomuser.me/api/”时,我们得到了
TEST {"results":[{"gender":"female","name":{"title":"mrs","first":"milja","last":"makinen"},"location":{"street":"7070 pispalan valtatie","city":"geta","state":"central finland","postcode":24464},"email":"milja.makinen@example.com","login":{"username":"brownwolf135","password":"triton","salt":"Ahx7UsGp","md5":"aa3abe69ad2001470c00de947d112270","sha1":"cc0db5a4ecd489d6f53876ae143cdab117edbba3","sha256":"488268929cd6da0e50fbb61666ddd03136a3ad16914822a650c5d134db9364af"},"registered":1031987491,"dob":908944207,"phone":"05-992-273","cell":"046-791-11-07","id":{"name":"HETU","value":"60770785-G"},"picture":{"large":"https://randomuser.me/api/portraits/women/73.jpg","medium":"https://randomuser.me/api/portraits/med/women/73.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/73.jpg"},"nat":"FI"}],"info":{"seed":"ea5f8a5a02e09d48","results":1,"page":1,"version":"1.0"}}
如您所见,不是括号。我们真的是第一个发现这个的人,还是我们错过了一些完全明显的东西?
答案 0 :(得分:1)
您是否尝试提供Accept-Header(https://circleci.com/docs/api/#accept-header)?
如果您指定不接受标头,我们将返回带有注释的人类可读JSON。如果您希望接收没有空格或注释的紧凑JSON,请添加" application / json"接受标题