29 stable releases (12 major)

15.1.0 Sep 30, 2020
14.2.0 Jun 3, 2020
14.0.6 Feb 7, 2020
14.0.5 Nov 28, 2019
0.1.0 Jan 19, 2016

#7 in HTTP server

Download history 7161/week @ 2020-08-11 6500/week @ 2020-08-18 6657/week @ 2020-08-25 5171/week @ 2020-09-01 7279/week @ 2020-09-08 8316/week @ 2020-09-15 7038/week @ 2020-09-22 5951/week @ 2020-09-29 6367/week @ 2020-10-06 7600/week @ 2020-10-13 6977/week @ 2020-10-20 7805/week @ 2020-10-27 6590/week @ 2020-11-03 6277/week @ 2020-11-10 7389/week @ 2020-11-17 6545/week @ 2020-11-24

28,855 downloads per month
Used in 42 crates (18 directly)

MIT license

185KB
5.5K SLoC

jsonrpc-http-server

Rust http server using JSON-RPC 2.0.

Documentation

Example

Cargo.toml

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

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

~9.5MB
~197K SLoC