#json-rpc #http-server #rpc-server #server #rpc #json-rpc-server

tetsy-jsonrpc-http-server

Tetsy Rust http server using JSONRPC 2.0

Show the crate…

3 stable releases

15.1.0 Mar 13, 2021
14.2.1 Mar 1, 2021

#36 in #jsonrpc

Download history 10/week @ 2023-12-07 17/week @ 2023-12-14 17/week @ 2023-12-21 4/week @ 2023-12-28 15/week @ 2024-01-04 44/week @ 2024-01-11 10/week @ 2024-01-18 4/week @ 2024-01-25 2/week @ 2024-02-01 13/week @ 2024-02-08 31/week @ 2024-02-15 36/week @ 2024-02-22 35/week @ 2024-02-29 39/week @ 2024-03-07 32/week @ 2024-03-14 28/week @ 2024-03-21

145 downloads per month
Used in 25 crates (2 directly)

MIT license

190KB
5.5K SLoC

tetsy-jsonrpc-http-server

Rust http server using JSON-RPC 2.0.

Documentation

Example

Cargo.toml

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

main.rs

use tetsy_jsonrpc_http_server::*;
use tetsy_jsonrpc_http_server::tetsy_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

~14MB
~249K SLoC