Questions tagged [rust-axum]
80 questions
8
votes
1 answer
How to enable logging/tracing with Axum?
Learning Axum and I will like to add logging to a service I have put together, but unfortunately I cannot get it to work.
What I have done?
I added tower-http = { version = "0.3.5", features = ["trace"] } to Cargo.html and in the definition of the…

Finlay Weber
- 2,989
- 3
- 17
- 37
8
votes
1 answer
How to design an Axum server with test-friendliness in mind?
When I tried to build an application with axum, I failed to separate the framework from my handler. With Go, the classic way is define an Interface, implement it and register the handler to framework. In this way, it's easy to provide a mock handler…

Miss Yoimiya's puppy
- 143
- 1
- 6
7
votes
1 answer
How to log and filter requests with Axum/Tokio?
I am using Axum for a relatively simple Web API and would like to get a logging/tracing output for incoming requests similar to Go Gin, IIS logs, Python FastAPI, etc. - a simple path and parameters output.
The layer for HTTP is added to the…

Alen Siljak
- 2,482
- 2
- 24
- 29
6
votes
0 answers
Why am I getting "implementation of Deref is not general enough"?
Here's my minimum reproducible example:
use axum::{extract::State, Json, Router};
use diesel::pg::Pg;
use diesel_async::{
pooled_connection::bb8::Pool,
AsyncConnection, AsyncPgConnection, RunQueryDsl,
};
fn main() {
// body omitted for…

laptou
- 6,389
- 2
- 28
- 59
6
votes
1 answer
How to make an axum router handle return different content-type responses?
For example, when user access http://127.0.0.1:8080/hello, if query parameter id is 1, a plain text response return. If id is 2, give a json structure.
Summary:
id (input)
status code
content-type
body
1
200
application/json
{"name":…

progquester
- 1,228
- 14
- 23
6
votes
1 answer
How to call struct method from axum server route?
I want to instantiate a struct instance and then call a method of that instance in an api route. Here's an example of what I want, but it results in an error:
use axum::{http::StatusCode, routing::get, Router, Server};
#[derive(Clone)]
struct Api…

SomeRandomPhysicist
- 1,531
- 4
- 19
- 42
5
votes
0 answers
Problem with reqwest as client and axum as server (axum blows up at 1k req/s with port in use error)
I have problem, why do I get this error when sending request
Error: reqwest::Error {
kind: Request,
url: Url {
scheme: "http",
cannot_be_a_base: false,
username: "",
password: None,
host:…

SildCave
- 51
- 5
4
votes
0 answers
Axum does no redirect when being in Root Route when redirecting from / and with routing normalization
I am trying to make a multilingual website that redirects you to the homepage in the corresponding language when accessing it's / route, but for some reason Axum is no being able to redirect, but only when setting the redirection from the / route…

Axum500
- 41
- 2
4
votes
1 answer
Route paths with or without of trailing slashes in rust/axum
I want to route both http://0.0.0.0/foo and http:0.0.0.0/foo/ to the same get_foo handler. However, in practice, only /foo gets routed and /foo/ 404s. I suspect I'm setting up/attaching the middleware wrong:
use axum::http::StatusCode;
use…

kindrobot
- 1,309
- 1
- 10
- 19
4
votes
1 answer
Shared state doesn't work because of lifetimes
Im using the Axum framework to build a simple rest server. I want to have a sort of an "App State" that will have some reusable components for some of my handlers to reuse/consume.
#![allow(incomplete_features)]
#![feature(async_fn_in_trait)]
use…

Gilbert Nwaiwu
- 687
- 2
- 13
- 37
4
votes
1 answer
How to use both axum::extract::Query and axum::extract::State with Axum?
I am building an handler that requires state to be injected and also needs to extract the query parameters.
I started off by only extracting the state and that worked. The code for that looks something like this:
#[derive(ValueEnum, Clone,…

Finlay Weber
- 2,989
- 3
- 17
- 37
3
votes
0 answers
higher-ranked lifetime error for Axum post
pretty new to rust and trying to write a simple web server using Axum. I encountered the following error during compilation:
error: higher-ranked lifetime error
|
16 | .route("/login", post(login));
| …

Ruster
- 37
- 4
3
votes
1 answer
Rust Axum Multipart Length Limit Exceeded
Referenced Axum documentation: docs.rs
Hello all, I am trying to create a simple file upload using HTML5 forms and Rust Axum.
The issue is that, while any normal file works, larger files (particularly video files) which I want to upload are too…

Ty Q.
- 207
- 2
- 13
3
votes
1 answer
Axum: pass parameters to handlers
new to rust here. I am trying to build a simple server that provides and decodes JWT tokens and I am missing a big part. Here is the code:
pub struct Server {
pub host: String,
pub port: String,
pub public_key: String,
pub…

Francesco
- 1,742
- 5
- 44
- 78
3
votes
1 answer
How to return contents as a file download in Axum?
I have a Rust application that is acting as a proxy. From the user perspective there is a web UI front end. This contains a button that when invoked will trigger a GET request to the Rust application. This in turn calls an external endpoint that…

JGilmartin
- 8,683
- 14
- 66
- 85