0

I need to split value of array like

let v: Vec<&str> = token.split("Bearer ").collect();

and log the value of the token the value is Bearer eyJGciOiJSU and I need to get only eyJGciOiJSU when I print the v[1] to the log I got error index out of range, any idea?

2

1 Answer 1

4

Hard to assess without a minimal example, but this works on the playground:

fn main() {
    let token = "Bearer eyJGciOiJSU";
    let v: Vec<_> = token.split("Bearer ").collect();
    println!("{}", v[1]); // prints "eyJGciOiJSU"
    
    let id = token.strip_prefix("Bearer ").expect(r#"Token ought to start with "Bearer""#);
    println!("{}", id); // prints "eyJGciOiJSU"
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot! one question , I need to get the payload eg name from the jwt token any idea how to do it? play.rust-lang.org/…
this is the jwt example jwt.io , I need at the end to get the ` "name": "John Doe"` from the payload, could you please assist? `
@JennyM You might want to have a look at this example from the jasonwebtoken docs. You should just need to replace the Claims struct with a struct that matches your payload type. I.e. something like struct Payload { sub: String, name: String, iat: usize }.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.