33 stable releases (15 major)

18.0.0 Jul 20, 2021
17.1.0 Jun 7, 2021
17.0.0 Jan 20, 2021
16.0.0 Dec 14, 2020
0.1.0 Jan 19, 2016

#17 in HTTP server

Download history 17603/week @ 2024-03-14 19816/week @ 2024-03-21 19323/week @ 2024-03-28 21557/week @ 2024-04-04 22998/week @ 2024-04-11 24866/week @ 2024-04-18 21684/week @ 2024-04-25 21182/week @ 2024-05-02 21253/week @ 2024-05-09 19155/week @ 2024-05-16 16060/week @ 2024-05-23 19845/week @ 2024-05-30 21257/week @ 2024-06-06 21319/week @ 2024-06-13 19192/week @ 2024-06-20 15855/week @ 2024-06-27

81,680 downloads per month
Used in 104 crates (32 directly)

MIT license

190KB
5.5K SLoC

jsonrpc-http-server

Rust http server using JSON-RPC 2.0.

Documentation

Example

Cargo.toml

[dependencies]
jsonrpc-http-server = "15.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

~10–21MB
~277K SLoC