Questions tagged [actix-web]
488 questions
6
votes
1 answer
How to return the error description in a invalid JSON request body to the client in Rust?
In Python, I can use marshmallow or Pydantic to validate user input just by defining a schema (much like a Rust struct). Then using that schema, Marshmallow loads the user input and returns the error it finds. Such as:
I can have custom error…

azzamsa
- 1,805
- 2
- 20
- 28
6
votes
1 answer
How to use async code in actix-web extractors?
I am implementing an authentication extractor in actix-web 2.0.0 using sqlx to access the database. I have this code:
use actix_web::{dev, web, Error, HttpRequest, FromRequest};
use actix_web::error::ErrorUnauthorized;
use futures::future::{ok, err,…

JoeCamel
- 672
- 4
- 16
6
votes
1 answer
How to get Cookie from Request in Actix 2.0
I want to get cookie's value from request. I found that in Actix 0.x.x, cookie's value could be get from calling
fn get_cookie(req: HttpRequest) {
let cookie = req.cookie("name") <-- Here
return HttpResponse::Ok()
.body(
…

SaltyAom
- 81
- 2
- 4
6
votes
1 answer
actix_web middleware ErrorHandlers return error message in ServiceResponse
I am trying to capture errors that may occur for requests made to my server.
This came up when I was receiving a 400 on one of my POST requests (which was thrown before even getting to my request handler method) and I was receiving no feedback as…

rpascal
- 723
- 1
- 8
- 25
6
votes
3 answers
How to use routes attributes macros for multiple methods in Actix-Web
In the Actix Web Framework, how does one use the route attributes macros (#[http_method("route")]) to bind multiple http methods to one function?
For example, I have this trivial endpoint:
/// Returns a UUID4.
#[get("/uuid")]
async fn uuid_v4() ->…

mattgathu
- 1,129
- 1
- 19
- 28
6
votes
1 answer
Why does my shared actix-web state sometimes reset back to the original value?
I'm trying to implement shared state in the Actix-Web framework with Arc and Mutex. The following code compiles, but when I run it, the counter sometimes goes all the way back to 0. How do I prevent that from happening?
use actix_web::{web, App,…

HelloWorld
- 973
- 2
- 9
- 23
6
votes
3 answers
How to read a request's body in an actix-web 1.0 middleware?
I want to read out the body in a middleware in actix-web 1.0. I'm using the closure-style middleware using wrap_fn.
My basic setup is this:
let mut server = HttpServer::new(move || {
ActixApp::new()
.wrap_fn(|req, srv| {
…

svenstaro
- 1,783
- 2
- 21
- 31
6
votes
2 answers
How to use pooling of database inside function using actix?
I created a pool for MySQL database like this:
let database_url = env::var("DATABASE_URL").expect("set DATABASE_URL");
let manager = ConnectionManager::::new(database_url);
let pool = r2d2::Pool::builder()
.build(manager)
…

Cliff Anger
- 195
- 2
- 11
5
votes
1 answer
'Access-Control-Allow-Origin' missing using actix-web
Stuck on this problem where I received this error everytime making POST request to my actix-web server.
CORS header 'Access-Control-Allow-Origin' missing
my javascript (VueJs running on localhost:3000) :
let data = //some json data
let xhr = new…

Muhammad Najid
- 63
- 5
5
votes
2 answers
Error: The trait `Handler<_>` is not implemented for `fn() -> HttpResponse
If I was to use this code on Actix Web 3 it would work, but I need to use the latest stable release... so 4^.
Here is the snippet in question (actually this is the entirety of my code):
use actix_web::{web, App, HttpResponse, HttpServer,…

Xavi Font
- 296
- 4
- 19
5
votes
1 answer
What is the role of a "Extension" in actix-web?
I'm trying to figure out how Extensions are created in the actix-web Rust library. I think I'm missing the concept of request extensions. What do request extensions do? How are they different from HTTP headers?

pandawithcat
- 571
- 2
- 13
5
votes
1 answer
How to share reqwest clients in actix-web server?
I'm building a web server with actix-web, and one of the methods uses reqwest to make HTTP requests to an external API:
#[get("/foo")]
async fn foo() -> impl Responder {
let resp = reqwest::get("https://externalapi.com/bar").await?; # GET request…

tamuhey
- 2,904
- 3
- 21
- 50
5
votes
1 answer
Actix Web is not handling post request?
So I'm trying to create a basic actix-web application that will allow me to create a very basic blog system. It is handling my GET requests, but it's not handling my POST requests.
main.rs:
use actix_web::{HttpServer, App, web};
use…

Alchemist
- 63
- 1
- 5
5
votes
2 answers
Actix-Web: Run service every 10 seconds
I'm currently implementing a server using Rust and Actix-Web. My task now is to send a request (ping-request) from this server to another server every 10 seconds. The ping-request itself is implemented in a async function:
async fn ping(client:…

Samuel Dressel
- 1,181
- 2
- 13
- 27
5
votes
1 answer
How can I receive multiple query params with the same name in actix-web?
In the actix-web documentation is only an example of how to receive uniquely named query params.
But how can I receive multiple query params of the same name? For example:
http://localhost:8088/test?id=1&id=2&id=3
How do I have to change following…

zingi
- 1,141
- 13
- 23