比特币 cURL/JSON-RPC 使用 Rust
Bitcoin cURL/JSON-RPC using Rust
我正在用 RUST 编写代码,使用 JSON-RPC 从 bitcoin-core 查询数据。我正在使用此 curl-rust,但 运行 cargo run
.
上未显示任何输出
extern crate curl;
use std::io::Read;
use curl::easy::{Easy, List};
fn main() {
let mut data = r#"{"jsonrpc":"1.0","id":"curltext","method":"getrawtransaction","params":["f8ae07a1292136def6d79d5aef15aacfa1aefa2db153037b878b06f00e2cd051", 2]}"#.as_bytes();
let mut easy = Easy::new();
easy.url("http://192.168.X.X:8332").unwrap();
easy.post(true).unwrap();
easy.post_field_size(data.len() as u64).unwrap();
let mut list = List::new();
list.append("Authorization: Basic some_user:some_password").unwrap();
easy.http_headers(list).unwrap();
let mut transfer = easy.transfer();
transfer.read_function(|buf| {
Ok(data.read(buf).unwrap_or(0))
}).unwrap();
transfer.perform().unwrap();
}
我希望代码能提供一些输出,为什么不是这样?
您未接 write_function
来电。一个例子:
use std::io::Read;
use curl::easy::Easy;
fn main() {
let mut body = r#"{"jsonrpc":"2.0","method":"guru.test","params":["Guru"],"id":123}"#.as_bytes();
let mut easy = Easy::new();
easy.url("https://gurujsonrpc.appspot.com/guru").unwrap();
easy.post(true).unwrap();
easy.post_field_size(body.len() as u64).unwrap();
let mut data = Vec::new();
{
// Create transfer in separate scope ...
let mut transfer = easy.transfer();
// Request body
transfer.read_function(|buf| {
Ok(body.read(buf).unwrap_or(0))
}).unwrap();
// Response body
transfer.write_function(|new_data| {
data.extend_from_slice(new_data);
Ok(new_data.len())
}).unwrap();
transfer.perform().unwrap();
// .. to force drop it here, so we can use easy.response_code()
}
println!("{}", easy.response_code().unwrap());
println!("Received {} bytes", data.len());
if !data.is_empty() {
println!("Bytes: {:?}", data);
println!("As string: {}", String::from_utf8_lossy(&data));
}
}
我正在用 RUST 编写代码,使用 JSON-RPC 从 bitcoin-core 查询数据。我正在使用此 curl-rust,但 运行 cargo run
.
extern crate curl;
use std::io::Read;
use curl::easy::{Easy, List};
fn main() {
let mut data = r#"{"jsonrpc":"1.0","id":"curltext","method":"getrawtransaction","params":["f8ae07a1292136def6d79d5aef15aacfa1aefa2db153037b878b06f00e2cd051", 2]}"#.as_bytes();
let mut easy = Easy::new();
easy.url("http://192.168.X.X:8332").unwrap();
easy.post(true).unwrap();
easy.post_field_size(data.len() as u64).unwrap();
let mut list = List::new();
list.append("Authorization: Basic some_user:some_password").unwrap();
easy.http_headers(list).unwrap();
let mut transfer = easy.transfer();
transfer.read_function(|buf| {
Ok(data.read(buf).unwrap_or(0))
}).unwrap();
transfer.perform().unwrap();
}
我希望代码能提供一些输出,为什么不是这样?
您未接 write_function
来电。一个例子:
use std::io::Read;
use curl::easy::Easy;
fn main() {
let mut body = r#"{"jsonrpc":"2.0","method":"guru.test","params":["Guru"],"id":123}"#.as_bytes();
let mut easy = Easy::new();
easy.url("https://gurujsonrpc.appspot.com/guru").unwrap();
easy.post(true).unwrap();
easy.post_field_size(body.len() as u64).unwrap();
let mut data = Vec::new();
{
// Create transfer in separate scope ...
let mut transfer = easy.transfer();
// Request body
transfer.read_function(|buf| {
Ok(body.read(buf).unwrap_or(0))
}).unwrap();
// Response body
transfer.write_function(|new_data| {
data.extend_from_slice(new_data);
Ok(new_data.len())
}).unwrap();
transfer.perform().unwrap();
// .. to force drop it here, so we can use easy.response_code()
}
println!("{}", easy.response_code().unwrap());
println!("Received {} bytes", data.len());
if !data.is_empty() {
println!("Bytes: {:?}", data);
println!("As string: {}", String::from_utf8_lossy(&data));
}
}