NodeJS POST Request Over JSON-RPC

0 votes

I am trying to execute a POST request over JSON-RPC on my NodeJS server. Converting the following curl command:

curl -X POST --data '{"jsonrpc":"2.0","method":"personal_newAccount","params":["pass"],"id":74}' http://localhost:8545

In NodeJS I keep receiving:

200 {"id":-1,"jsonrpc":"2.0","error":{"code":-32600,"message":"Could not decode request"}}

In the header I am specifying the Content-Type. If someone can point out what I am not specifying and how to add it in it would be much appreciated.

var headers = {
    'User-Agent':       'Super Agent/0.0.1',
    'Content-Type':     'application/json-rpc',
    'Accept':'application/json-rpc'
}

var options = {
    url: "http://localhost:8545",
    method: 'POST',
    headers: headers,
    form: {"jsonrpc":"2.0","method":"personal_newAccount","params":["pass"],"id":1}
}

request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        res.writeHeader(200, {"Content-Type": "text/plain"});
        res.write(res.statusCode.toString() + " " + body);
    }else{
      res.writeHeader(response.statusCode, {"Content-Type": "text/plain"});
      res.write(response.statusCode.toString() + " " + error);
    }
    res.end();
})
Sep 28, 2018 in Blockchain by slayer
• 29,370 points
2,375 views

1 answer to this question.

0 votes

You're missing the --header option:

curl --request POST \
    --header 'Content-type: application/json' \
    --data '{"jsonrpc":"2.0","method":"personal_newAccount","params":["pass"],"id":74}' \
    http://localhost:8545
answered Sep 28, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
1 answer

Ethereum nodejs: Unhandled rejection Error: Invalid JSON RPC response

gasLimit is actually represented by gas as the gas limiter ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,220 points
2,443 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
764 views
0 votes
1 answer

How to receive JSON object via POST method?

You need to decode it with json_decode() like this $json ...READ MORE

answered Aug 28, 2018 in Blockchain by slayer
• 29,370 points
871 views
0 votes
1 answer

How to make POST request on C# to blockr.io

By default UploadValues doesn't format the data ...READ MORE

answered Aug 30, 2018 in Blockchain by digger
• 26,740 points
1,754 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,456 views
0 votes
1 answer

Invalid Batch or signature in Savtooth

This will solve your problem import org.apache.commons.codec.binary.Hex; Transaction txn ...READ MORE

answered Aug 1, 2018 in Blockchain by digger
• 26,740 points
1,010 views
+1 vote
1 answer
0 votes
1 answer

Python request module for bitcoin json rpc

This should work: #!/usr/bin/env python import getpass import json import requests ...READ MORE

answered Aug 28, 2018 in Blockchain by digger
• 26,740 points
2,365 views
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,075 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