Skip to content

How in the world can I get an header? #2551

Answered by robertwayne
notv1x asked this question in Questions
Discussion options

You must be logged in to vote

Rocket provides a built-in guard for getting the IP Address, see here and here. This - under the hood - defaults to extracting the "X-Real-IP" header.

#[get("/")]
async fn get_ip(ip: std::net::IpAddr) -> String {
    format!("Your IP is: {}", ip)
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![get_ip])
}

However, if you wish to manually extract a header, you have access to .headers() in a request guard (see here). I feel like this example is quite self-explanatory, and could be modified to your use-case quite easily by changing just the header names. If you have questions on that specifically, feel free to ask.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by SergioBenitez
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants