为了方便查看, 先在这里拉一个表格出来。
返回码 | 命名 | 备注 |
202 | Accepted | |
400 | Bad Request | |
409 | Conflict | |
201 | Created | |
Custom | 自定义返回码 | |
403 | For bidden | |
204 | No Content | |
401 | Unauthorized |
自定义返回状态码样例
这里写了一个get网站根目录之后返回个携带418状态码, 同时body里带个json字符串。
#[derive(Responder)] #[response(status = 418, content_type = "json")] struct RawTeapotJson(&'static str); #[get("/")] fn json() -> RawTeapotJson { RawTeapotJson("{ \"hi\": \"world\" }") }
注意这里的Json不是那个serde::json::Json
想返回200怎么办?
官方文档上说应当: Empty with given status. 就是什么Status都别写。