0

I have a POST endpoint:

use rocket::serde::json::Json;
use rocket::{ State, Request };

#[post(
    "/my_endpoint",
    format = "json",
    data = "<json_my_data>"
)]
pub async fn my_endpoint(
    request: &Request<'_>,
    peer_state: &State<Arc<peers::MyPeer>>,
    json_my_data: Json<models::JsonMyData>,
) 

I want to get the IP address of the endpoint's caller. I was planning to use Rocket's Request:

let ip_address = match request.client_ip() {
  Some(ip) => ip.to_string(),
  None => {
    error!("Invalid IP Address");
    return;
  },
};

But I get the following compilation error:

the trait bound `&rocket::Request<'_>: FromRequest<'_>` is not satisfied

I'm using the following Rocket version in my Cargo.toml:

rocket = { git = "https://github.com/LIT-Protocol/Rocket", branch = "feature/port-reuse", features = ["json"] }

Is there a way to get the IP address of the caller without manually implementing the FromRequest for the request param?

NewToCode
  • 174
  • 8
  • Any reason you don't just take a [`IpAddr`](https://doc.rust-lang.org/nightly/core/net/enum.IpAddr.html) parameter? That gives you the `client_ip` as per the [Provided Implementations](https://api.rocket.rs/v0.5-rc/rocket/request/trait.FromRequest.html#provided-implementations) – cafce25 Jul 24 '23 at 14:45
  • Hi @cafce25 how can I also get the port info? – NewToCode Jul 24 '23 at 15:15
  • The port info that's not even in the `Request`? Seems unlikely. – cafce25 Jul 24 '23 at 15:49

0 Answers0