2

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
})
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Attilah
  • 17,632
  • 38
  • 139
  • 202
  • Have you tried [`status`](https://docs.rs/actix-web/3.3.2/actix_web/dev/struct.HttpResponseBuilder.html#method.status)? – E_net4 Apr 28 '21 at 11:17

1 Answers1

3

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,
    )
}
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841