<p>Body must be a string.</p>

<p>You can use form instead of body.</p>

<p>Reference: 
<a href="https://www.npmjs.com/package/request#forms" rel="nofollow noreferrer">https://www.npmjs.com/package/request#forms</a></p>

CC BY-SA 3.0
2018-02-05T15:22:49.290Z
2018-02-05T15:22:49.290Z
siddolo
<p>I'm using BitcoinJS to create a playground for Bitcoin transactions.
I have created the following Javascript file:</p>

<pre><code>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 = &lt;my private key&gt;;
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(&lt;one of my input transactions&gt;, 0);

tx.addOutput(&lt;destination public key&gt;, amountToSend);
tx.addOutput(publicKey, amountToKeep);
tx.sign(0, wallet);

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

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

<p>Here is my output:</p>

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

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

<testnet><raw-transaction><bitcoinjs><createrawtransaction><request>
How to push a raw transaction using BitcoinJS (and Request)?
smartmouse
# How to push a raw transaction using BitcoinJS (and Request)?

(Asked by: username_0 on 2018-02-05)

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();

proxiedRequest.post({
    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...

**Tags:** testnet, raw-transaction, bitcoinjs, createrawtransaction, request


---

## 1 answers

### Answer by username_1 on 2018-02-05. Score: 3 | Accepted answer

Body must be a string.

You can use form instead of body.

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


---

