Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the status code of a response?

Tags:

rust

actix-web

I'm using actix for a web application I'm working on, and I'm trying to set the status code 409 to a response, but I don't know how to do that. Something a bit like this:

HttpResponse::StatusCode(409).json(Status{
            value: val,
            isOn: true
})
like image 604
Attilah Avatar asked Feb 01 '26 19:02

Attilah


1 Answers

There are several ways to explicitly set the status code. The easiest way is to make use of actix-web's implementation of Responder for (T, StatusCode) whenever T: Responder. Assuming Status is some custom struct implementing Serialize, you should be able to write something like

use actix_web::{HttpRequest, Responder, http, web};

fn foo(req: HttpRequest) -> impl Responder {
    (
        web::Json(Status {
            value: val,
            isOn: true,
        }),
        http::StatusCode::CONFLICT,
    )
}
like image 187
Sven Marnach Avatar answered Feb 03 '26 08:02

Sven Marnach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!