28 stable releases (12 major)

new 15.0.0 Sep 18, 2020
14.2.0 Jun 3, 2020
14.1.0 Apr 14, 2020
14.0.6 Feb 7, 2020
0.1.0 Jan 19, 2016

#9 in HTTP server

Download history 6334/week @ 2020-06-01 6937/week @ 2020-06-08 6744/week @ 2020-06-15 6142/week @ 2020-06-22 6104/week @ 2020-06-29 6523/week @ 2020-07-06 6091/week @ 2020-07-13 6576/week @ 2020-07-20 6923/week @ 2020-07-27 7508/week @ 2020-08-03 7519/week @ 2020-08-10 6311/week @ 2020-08-17 6846/week @ 2020-08-24 5495/week @ 2020-08-31 6682/week @ 2020-09-07 7645/week @ 2020-09-14

28,615 downloads per month
Used in 39 crates (15 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