IOTA API手册

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

broadcastTransactions - 广播交易

broadcastTransactions调用广播一组指定的三进制交易码流。

调用参数

  • trytes:有效的三进制交易码流数组,必须

trytes指定的交易码流必须已经包含有之前使用attachToTangle调用 计算的POW。

返回结果

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

  • duration:完成请求花费的时间,单位:毫秒

调用成功时返回HTTP状态码200,示例如下:

{
  "duration": 567
}

调用失败时返回HTTP状态码400,示例如下:

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

示例代码

Python

import urllib2
import json

command = {
  "command": "broadcastTransactions",
  "trytes": ["P9KFSJVGSPLXAEBJSHWFZLGP ..."]
}

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

Node.js

var request = require('request');

var command = {
  "command": "broadcastTransactions",
  "trytes": ["P9KFSJVGSPLXAEBJSHWFZLGP ..."]
  }

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": "broadcastTransactions",
  "trytes": ["P9KFSJVGSPLXAEBJSHWFZLGP ..."]
  }'