[Ethereum] message: ‘invalid argument 0: json: cannot unmarshal hex string without 0x prefix into Go struct field SendTxArgs.value of type *hexutil.Big’

go-ethereumjavascriptjson-rpcsendtransactiontransactions

I am using a JSON RPC request and source code is as follows:

var options ={
    url: "http://127.0.0.1:8545",
    method: "POST",
    headers: {"Content-Type":"application/json"}   
};
var tx = {from: req.body.fromaddress,  to: req.body.toaddress,  value: web3.utils.toWei(req.body.amount, "ether")};
    options.body = JSON.stringify({"jsonrpc":"2.0", "method":"personal_sendTransaction", "params":[tx, req.body.password], "id":"curltest"});
    callback = (err, response, body) => {
        if (!err && response.statusCode == 200) {
            const data = JSON.parse(body);
            console.log(data);
            res.send(data);
        }
        if (err) {
            console.error(err);
            res.send(err);
        }
    };
    request(options, callback);

Best Answer

Your value field of tx must be pre-pended with 0x. If the value is 0, then tx.value must be 0x0..

Related Topic