Tokio Tungestenite - Get request params

Hello I am trying to use tokio-tungstenite to spin up a websocket server. On a request to the server from the client, I want to parse the query params from the request.

Currently I am using the accept_hdr_async function where I can access the request URI and get the query params. However, once the handshake is done, I can't see a way to get the request URI still.

Does anyone have insights on how to get around this? Looks like something along this (tokio_tungstenite::client_async - Rust) maybe handy but this is client-side unfortunately. Thanks!

You can just store the URI in a mutable variable:

let mut uri = None;
let ws_stream = accept_hdr_async(stream, |req, res| {
    uri = Some(req.uri().clone());
    Ok(res)
}).await;
let uri = uri.unwrap();
1 Like

I am not sure if I can do that though because the function expects a type that implements the Callback trait. The Callback trait has a on_request function which takes in the req and res

I'm pretty sure @Kestrer's closure would implement the Callback trait.

Interesting I was getting an error but then got a hint from this post: Confusing type error due to strange inferred type for a closure argument · Issue #41078 · rust-lang/rust · GitHub

I added the types and it's compiling now. Thank you!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.