24 stable releases (11 major)

✓ Uses Rust 2018 edition

new 14.0.1 Oct 18, 2019
13.2.0 Sep 13, 2019
13.0.0 Jul 29, 2019
12.2.0 Jul 30, 2019
0.1.0 Jan 19, 2016

#3 in #server

Download history 1455/week @ 2019-07-05 1211/week @ 2019-07-12 1355/week @ 2019-07-19 1406/week @ 2019-07-26 1472/week @ 2019-08-02 1967/week @ 2019-08-09 2181/week @ 2019-08-16 2885/week @ 2019-08-23 2641/week @ 2019-08-30 2610/week @ 2019-09-06 2475/week @ 2019-09-13 2892/week @ 2019-09-20 2521/week @ 2019-09-27 2878/week @ 2019-10-04 2930/week @ 2019-10-11

10,084 downloads per month
Used in 23 crates (12 directly)

MIT license

185KB
5K SLoC

jsonrpc-http-server

Rust http server using JSON-RPC 2.0.

Documentation

Example

Cargo.toml

[dependencies]
jsonrpc-http-server = "14.0"

main.rs

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();
}

You can now test the above server by running cargo run in one terminal, and from another terminal issue the following POST request to your server:

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

to which the server will respond with the following:

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

If you omit any of the fields above, or invoke a different method you will get an informative error message:

$ 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}

Dependencies

~7.5MB
~166K SLoC