expression
REST: return HTTP 429 with Retry-After
When a client exceeds a request quota, a service can respond with HTTP 429. An optional Retry-After header can indicate how long to wait; this example supplies five seconds. The actual delay must come from the service policy. Standalone synthetic example. Expected stdout (not executed): 429 5 Common mistake: Returning a successful response for a rejected request or confusing the retry delay with milliseconds.
Formal Syntax & Structure
rust syntaxrust
(StatusCode::TOO_MANY_REQUESTS, [(RETRY_AFTER, "5")], body)Working Example
Example Snippetrust
use axum::{Router, Json, body::{Body, to_bytes}, http::{Request, StatusCode}};use serde::{Deserialize, Serialize};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() { use axum::{http::header::RETRY_AFTER, response::IntoResponse}; let res = (StatusCode::TOO_MANY_REQUESTS, [(RETRY_AFTER, "5")], "quota exceeded").into_response(); println!("{} {}", res.status().as_u16(), res.headers()[RETRY_AFTER].to_str().unwrap());}