gotham-rs / gotham

A flexible web framework that promotes stability, safety, security and speed.

Home Page:https://gotham.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add examples about how to use `status_mut()` to change the status code of a response

heliomar-pena opened this issue · comments

The problem

I was looking for a way to change the status code of my response and it has been hard to find to me, I based on the next example to create a CORS middleware, but the response status code was always 200 code, instead of 401 (unauthorized), so I had to change the status code manually, but I don't find any way to change it properly.

let f = result.and_then(move |(state, mut response)| {
{
let headers = response.headers_mut();
let data = ExampleMiddlewareData::borrow_from(&state);
// All our middleware does is add a header to the Response generated by our handler.
headers.insert(
"X-User-Agent",
format!(
"Supplied: {}, Supported: {}",
data.user_agent, data.supported
)
.parse()
.unwrap(),
);
};
future::ok((state, response))
});

I also tried to base on this documentation to change the status code, but that doesn't work with my problem, since this example instead of modifying an existing response is creating a new empty response.

pub fn middleware_reliant_handler(mut state: State) -> (State, Response<Body>) {
{
let data = ExampleMiddlewareData::borrow_mut_from(&mut state);
// Mark any kind of web client as supported. A trival example but it highlights the
// interaction that is possible between Middleware and Handlers via state.
data.supported = true;
};
// Finally we create a basic Response to complete our handling of the Request.
let res = create_empty_response(&state, StatusCode::OK);
(state, res)
}

Then I noticed the status_mut function in the response struct, but it doesn't have any documentation about how to use it. So I couldn't use it anyway. Later I checked and found this block of code in this GitHub repository and I based on it to change the response status code, and that worked properly. But I think it could be included in the examples, for example, the first one that I mentioned could include this part about how to change the status code

fn extend(state: &mut ::gotham::state::State, res: &mut ::gotham::hyper::Response<Self::ResBody>) {
res.headers_mut().insert(::gotham::helpers::http::header::X_REQUEST_ID,
::gotham::state::request_id(state).parse().unwrap());
*res.status_mut() = ::gotham::hyper::StatusCode::BAD_REQUEST;
}

The request

I suggest to add examples about how to use status_mut() to the middleware examples or to another example where it was visible so beginners can easily change the status_mut of a response

commented

The status_mut function is defined in the http crate and has documentation, including an example, on how to use it: https://docs.rs/http/0.2.8/http/response/struct.Response.html#method.status_mut

The status_mut function is defined in the http crate and has documentation, including an example, on how to use it: https://docs.rs/http/0.2.8/http/response/struct.Response.html#method.status_mut

It looks good, and what do you think about including it in the middleware examples? I meant, it's a common case and at the first is not clear how to do it