IOTA API手册

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

addNeighbors - 添加邻节点

使用addNeighbors调用为节点添加一组临时性的邻居,当节点重启后, 这些邻居节点将被删除。如果你希望为自己的节点添加永久性的邻居节点, 那么需要在NEIGHBORS配置选项中添加这些节点的URI。

调用参数

节点URI的格式为:"tcp://IPADDRESS:PORT"。

  • uris:要添加的邻居节点的URI数组,必须

返回结果

addNeighbors调用返回一个JSON对象,结构如下:

  • addedNeighbors:所添加节点的总数量
  • duration:完成请求的总用时,单位:毫秒

成功响应时的HTTP状态码为200,返回结果示例如下:

{
  "addedNeighbors": 2,
  "duration": 125
}

失败时的HTTP状态码为400,返回结果示例如下:

{
  "error": "'command' parameter has not been specified"
}

示例代码

Python

import urllib2
import json

command = {
  "command": "addNeighbors",
  "uris": [
    "tcp://8.8.8.8:14265",
    "tcp://8.8.8.8:14265"
  ]
}

stringified = json.dumps(command)

headers = {
    'content-type': 'application/json',
    'X-IOTA-API-Version': '1'
}

request = urllib2.Request(url="http://localhost:14265", data=stringified, headers=headers)
returnData = urllib2.urlopen(request).read()

jsonData = json.loads(returnData)

print jsonData

NodeJS

var request = require('request');

var command = {
  "command": "addNeighbors",
  "uris": [
    "tcp://8.8.8.8:14265",
    "tcp://8.8.8.8:14265"
  ]
}

var options = {
  url: 'http://localhost:14265',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
        'X-IOTA-API-Version': '1',
    'Content-Length': Buffer.byteLength(JSON.stringify(command))
  },
  json: command
};

request(options, function (error, response, data) {
  if (!error && response.statusCode == 200) {
    console.log(data);
  }
});

命令行

curl http://localhost:14265 \
-X POST \
-H 'Content-Type: application/json' \
-H 'X-IOTA-API-Version: 1' \
-d '{
  "command": "addNeighbors",
  "uris": [
    "tcp://8.8.8.8:14265",
    "tcp://8.8.8.8:14265"
  ]
}'