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.
gotham/examples/middleware/introduction/src/main.rs
Lines 75 to 92 in 84c7f08
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.
gotham/examples/middleware/introduction_await/src/main.rs
Lines 100 to 112 in 84c7f08
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
gotham/gotham_derive/src/extenders.rs
Lines 15 to 19 in 7251955
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
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 thehttp
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