I'm using BitcoinJS to create a playground for Bitcoin transactions. I have created the following Javascript file:

const request = require('request');
const proxiedRequest = request.defaults({proxy: "http://localhost:3128"});
const Btc = require('bitcoinjs-lib');
const TestNet = Btc.networks.testnet;
const apiUrl = 'https://testnet.blockexplorer.com/api/addr/';

let privateKey = <my private key>;
let wallet = new Btc.ECPair.fromWIF(privateKey, TestNet);
let publicKey = wallet.getAddress();
console.log("my public key:", publicKey);

let tx = new Btc.TransactionBuilder(TestNet);

let amountWeHave = 100000000; // 1.0 BTC
let amountToKeep = 90000000; // 0.9 BTC
let transactionFee = 1000; // 0.0001 BTC
let amountToSend = amountWeHave - amountToKeep - transactionFee; // ~0.1 (0.0999)

tx.addInput(<one of my input transactions>, 0);

tx.addOutput(<destination public key>, amountToSend);
tx.addOutput(publicKey, amountToKeep);
tx.sign(0, wallet);

let tx_hex = tx.build().toHex();

    headers: {'content-type' : 'multipart/mixed'},
    url:     'https://testnet.blockexplorer.com/api/tx/send',
    body:    tx_hex
}, (error, response, body) => {
    console.log('error', error);
    console.log('body', body);

Here is my output:

my public key: <my public key>
error null
body TypeError: Cannot read property &#39;toString&#39; of undefined<br> &nbsp; &nbsp;at Array.types.str (/home/bitcore/lib/bitcore-node/node_modules/bitcoind-rpc/lib/index.js:260:17)<br> &nbsp; &nbsp;at RpcClient.&lt;anonymous&gt; (/home/bitcore/lib/bitcore-node/node_modules/bitcoind-rpc/lib/index.js:236:35)<br> &nbsp; &nbsp;at Bitcoin.sendTransaction (/home/bitcore/lib/bitcore-node/lib/services/bitcoind.js:1791:15)<br> &nbsp; &nbsp;at Node.self.(anonymous function) [as sendTransaction] (/home/bitcore/lib/bitcore-node/lib/node.js:223:27)<br> &nbsp; &nbsp;at TxController.send (/home/bitcore/dev/insight-api/lib/transactions.js:290:13)<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/home/bitcore/lib/bitcore-node/node_modules/express/lib/router/layer.js:95:5)<br> &nbsp; &nbsp;at next (/home/bitcore/lib/bitcore-node/node_modules/express/lib/router/route.js:131:13)<br> &nbsp; &nbsp;at Route.dispatch (/home/bitcore/lib/bitcore-node/node_modules/express/lib/router/route.js:112:3)<br> &nbsp; &nbsp;at Layer.handle [as handle_request] (/home/bitcore/lib/bitcore-node/node_modules/express/lib/router/layer.js:95:5)<br> &nbsp; &nbsp;at /home/bitcore/lib/bitcore-node/node_modules/express/lib/router/index.js:277:22

Can you tell me what's wrong with my code? It seems to be a server problem...


Body must be a string.

You can use form instead of body.

Reference: https://www.npmjs.com/package/request#forms

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.