Query Params on Server macro #3510
-
Stack: leptos + axum
Upon checking axum, Query params can be accessed using extract in a function args, so does leptos but in a slightly different way. I did this:
This works, user_id_params = ":id" if i send a req that is exactly "/api/users/:id". If I do /api/users/2, server function is not found. I also tried /api/users/:{id} and /api/users/{id} on server macro endpoint, didn't work. I don't know how leptos work, so maybe I'm missing some things, if there are docs for these things, kindly point me in the right direction. If not, can someone help me on figuring this thing out? Though this might be an actix/axum specifics, but I hope that implementing these kind of things be included in the docs. thanks in advance |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
It's a bit hard to follow along with no code. But I think you're thinking about this the wrong way. What you're describing works fine with an ordinary Axum route, of course, and you can use one of those for whatever you want. "Server functions" are a particular abstraction though: if you want to take arguments like #[server]
pub async fn users(id: String) -> Result<User, ServerFnError> {
// ...
} |
Beta Was this translation helpful? Give feedback.
That's what I'm trying to say: "server functions" are an abstraction, where the arguments are encoded in specific ways. So, for a POST request, all of the arguments are encoded in the body of the request. For a GET request, all of the arguments are encoded as query params. etc.
So if you are defining a server function and you want to call it with a particular
id
, you do not pass it as part of the path: you pass it as the request body, just like an HTML<form method="POST">
would.If you want the flexibility to be able to do things like passing arguments via path params, you should just define an ordinary Axum route rather than trying to use a server function.