#json-rpc #rpc-server #server #rpc #http-server #json-rpc-server #json

已删除 solana-jsonrpc-http-server

Rust http服务器使用JSONRPC 2.0

使用旧的Rust 2015

0.4.0 2018年12月7日
0.3.0 2018年10月15日
0.2.0 2018年10月11日
0.1.2 2018年10月5日
0.1.1 2018年9月28日

#55 in #rpc-server

MIT 许可证

170KB
4.5K SLoC

jsonrpc-http-server

Rust http服务器使用JSON-RPC 2.0。

文档

示例

Cargo.toml

[dependencies]
jsonrpc-http-server = { git = "https://github.com/paritytech/jsonrpc" }

main.rs

extern crate jsonrpc_http_server;

use jsonrpc_http_server::*;
use jsonrpc_http_server::jsonrpc_core::*;

fn main() {
	let mut io = IoHandler::default();
	io.add_method("say_hello", |_| {
		Ok(Value::String("hello".into()))
	});

	let server = ServerBuilder::new(io)
		.cors(DomainsValidation::AllowOnly(vec![AccessControlAllowOrigin::Null]))
		.start_http(&"127.0.0.1:3030".parse().unwrap())
		.expect("Unable to start RPC server");

	server.wait();
}

您现在可以通过在一个终端中运行以下命令来测试上述服务器:cargo run,并在另一个终端中对服务器发出以下POST请求

$ curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "say_hello", "id":123 }' 127.0.0.1:3030

服务器将响应以下内容

{"jsonrpc":"2.0","result":"hello","id":123}

如果您省略上述任何字段或调用不同的方法,您将收到一个有用的错误消息

$ curl -X POST -H "Content-Type: application/json" -d '{"method": "say_hello", "id":123 }' 127.0.0.1:3030
{"error":{"code":-32600,"message":"Unsupported JSON-RPC protocol version"},"id":123}
$ curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "say_bye", "id":123 }' 127.0.0.1:3030
{"jsonrpc":"2.0","error":{"code":-32601,"message":"Method not found"},"id":123}

依赖项

~14MB
~239K SLoC