[英]NodeJS POST Request Over JSON-RPC
I am trying to execute a POST request over JSON-RPC on my NodeJS server. 我正在尝试在NodeJS服务器上通过JSON-RPC执行POST请求。 Converting the following curl command:
转换以下curl命令:
curl -X POST --data '{"jsonrpc":"2.0","method":"personal_newAccount","params":["pass"],"id":74}' http://localhost:8545
In NodeJS I keep receiving: 在NodeJS中,我不断收到:
200 {"id":-1,"jsonrpc":"2.0","error":{"code":-32600,"message":"Could not decode request"}}
In the header I am specifying the Content-Type. 在标题中,我指定了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();
})
You're missing the --header
option: 您缺少
--header
选项:
curl --request POST \
--header 'Content-type: application/json' \
--data '{"jsonrpc":"2.0","method":"personal_newAccount","params":["pass"],"id":74}' \
http://localhost:8545
form
is for application/x-www-url-encoded
requests, not JSON. form
适用于application/x-www-url-encoded
请求,而不适用于JSON。 Try these options instead: 请尝试以下选项:
var options = {
url: "http://localhost:8545",
method: 'POST',
headers: headers,
body: JSON.stringify({
jsonrpc: '2.0',
method: 'personal_newAccount',
params: ['pass'],
id: 1
})
}
You can also set json: true
in your options to have request
automatically parse the response as JSON. 您还可以在选项中设置
json: true
,以使request
自动将响应解析为JSON。
要使用“ personal_newAccount”和以太坊文档中的其余选项,您需要使用所需的API启动服务器:
--rpcapi "personal,eth,web3"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.