Using json rpc with curl on java

0 votes

i set the private ethereum on my local computer and run as

geth --bootnodes="enode://b115ff8b97f67a6bd8294a4ea277930bf7825e755705e809442885aba85e397313e46528fb662a3828cd4356f600c10599b77822ebd192199b6e5b8cfdb530c4@127.0.0.1:30303" --networkid 15 console --datadir "private-data" --rpcport "8545" --rpc --rpccorsdomain "*" --rpcapi "eth,web3,personal" --rpcaddr 192.168.44.114

and then i connect here with the remote computer's blockchain nodes

i want to use ethereum json rpc with curl on java .

i coded it as

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class shell{
public static void makeTran() throws Exception {


String shellcmd = "curl -X POST --data \"{\"jsonrpc\":\"2.0\",\"method\":\"personal_unlockAccount\",\"params\":[\"0xc7d863e8c89ac4b0336059b4e2cf84a57a6ba7db\", \"1\", 10],\"id\":1}\" http://192.168.44.114:8545/ -H \"Content-Type: application/json\"";
System.out.println(shellcmd);

Process process = Runtime.getRuntime().exec(shellcmd);

InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
    System.out.println(line);
}

}
    public static void main(String[] args) throws Exception {
    makeTran();
}
}

this must return this line

{"jsonrpc":"2.0","id":1,"result":true}

but this error is

curl -X POST --data "{"jsonrpc":"2.0","method":"personal_unlockAccount","params":["0xc7d863e8c89ac4b0336059b4e2cf84a57a6ba7db", "1", 10],"id":1}" http://192.168.44.114:8545/ -H "Content-Type: application/json"
invalid content type, only application/json is supported

here is command

curl -X POST --data '{"jsonrpc":"2.0","method":"personal_unlockAccount","params":["0xc7d863e8c89ac4b0336059b4e2cf84a57a6ba7db", "1", 10],"id":1}' http://192.168.44.114:8545/ -H 'Content-Type: application/json'

it can be run on terminal, but its not work on java

Oct 10, 2018 in Blockchain by slayer
• 29,370 points
2,724 views

1 answer to this question.

0 votes

Quoting different pieces the way you're doing doesn't work, so try splitting up the command-line arguments yourself. This code works:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Test {
    public static void main(String[] args) throws Exception {
        Process process = Runtime.getRuntime().exec(new String[] {
            "curl",
            "-H",
            "Content-Type: application/json",
            "--data",
            "{\"jsonrpc\":\"2.0\",\"method\":\"eth_blockNumber\",\"id\":1}",
            "https://mainnet.infura.io/",
        });

        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;

        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }
}

(That said, it's much better to just make the HTTP request from Java. There are plenty of resources available for how to do that.)

answered Oct 10, 2018 by Omkar
• 69,220 points

Related Questions In Blockchain

0 votes
1 answer

How to transact custom token instead of ethers using JSON rpc?

First create your custom token. Then develop ...READ MORE

answered Jul 10, 2018 in Blockchain by digger
• 26,740 points
1,074 views
0 votes
1 answer

How to interact with blockchain using Java web app?

You can interact with the blockchain using ...READ MORE

answered Jul 16, 2018 in Blockchain by digger
• 26,740 points
1,186 views
0 votes
1 answer
0 votes
1 answer

JAVA Retrive current bitcoin price with JSON

You should start log Exceptions when you ...READ MORE

answered Feb 28, 2022 in Blockchain by Soham
• 9,710 points
1,041 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,923 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
1,415 views
0 votes
1 answer

Why to use libraries over Json-RPC to interact with Blockchain?

Though Json-RPC provides methods to interact with ...READ MORE

answered Jan 11, 2019 in Blockchain by Omkar
• 69,220 points
763 views
0 votes
2 answers

How do I interact with a smart contract on a private network through web3.js

I found a blog that explains how ...READ MORE

answered Aug 20, 2018 in Blockchain by slayer
• 29,370 points
3,951 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP