17 releases (8 major breaking)

✓ Uses Rust 2018 edition

11.0.0 Apr 5, 2019
10.1.0 Feb 13, 2019
10.0.1 Jan 31, 2019
9.0.0 Nov 5, 2018
0.1.0 Jan 19, 2016

#5 in HTTP server

Download history 223/week @ 2018-12-21 140/week @ 2018-12-28 215/week @ 2019-01-04 196/week @ 2019-01-11 153/week @ 2019-01-18 91/week @ 2019-01-25 408/week @ 2019-02-01 683/week @ 2019-02-08 752/week @ 2019-02-15 759/week @ 2019-02-22 985/week @ 2019-03-01 871/week @ 2019-03-08 1113/week @ 2019-03-15 1179/week @ 2019-03-22 838/week @ 2019-03-29

2,754 downloads per month
Used in 16 crates (9 directly)

MIT license

172KB
5K SLoC

jsonrpc-http-server

Rust http server using JSON-RPC 2.0.

Documentation

Example

Cargo.toml

[dependencies]
jsonrpc-http-server = "11.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
~141K SLoC