I have create a server using rocket to access the peer information of an ipfs URL
The program as follows
#[macro_use] extern crate rocket;
use rocket::local::asynchronous::Client;
use rocket::http::hyper::{Request, Response};
use std::collections::HashMap;
use rocket::futures::TryFutureExt;
use std::fmt::Error;
use serde_json::Value;
#[get("/")]
async fn index(){
// https://github.com/rust-lang/rust/issues/63502
let client = reqwest::Client::new();
let data = r#"
{
"name": "ELKA",
"age": 43,
"phones": [
"+44 1234567",
"+44 2345678"
]
}"#;
let doc: Value = serde_json::from_str(data).unwrap();
let resp = client.post("https://_IPFS_URL:5001/api/v0/swarm/peers").send().await.unwrap();
// How to get the JSON response?
if resp.status().is_success() {
println!("success!");
println!("RESP: {:?}", resp);
} else if resp.status().is_server_error() {
println!("server error!");
println!("Something else happened. \n Resp {:?} \n Status: {:?}", resp, resp.status());
} else {
println!("Something else happened. \n Resp {:?} \n Status: {:?}", resp, resp.status());
}
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
}
Response from
curl -X POST https://_IPFS_URL:5001/api/v0/swarm/peers
is
{
"Peers":[
{
"Addr":"/ip4/172.31.20.195/tcp/4001",
"Peer":"183293203CY",
"Latency":"",
"Muxer":"",
"Direction":0,
"Streams":null
}
]
}
It's unclear to me how to read the response as a JSON using rust
I am using the reqwest
library
Would be grateful if anyone can give guidance.