Skip to content

Optional Path parameter #2874

Answered by robjtede
qarmin asked this question in Q&A
Sep 14, 2022 · 1 comments · 2 replies
Discussion options

You must be logged in to vote
#[get("/patient-info/{eye}")]
pub async fn AAAA(
    pool: web::Data<DbPool>,
    path: web::Path<String>,
) -> anyhow::Result<HttpResponse, Error> {
    Ok(HttpResponse::Ok().body("ABCD"))
}

Though Option<String> will not work here. You'll need two handlers and a delegate method. Eg:

#[get("/patient-info")]
pub async fn aaa(
    pool: web::Data<DbPool>,
    path: web::Path<String>,
) -> Result<HttpResponse, Error> {
  aaaa_inner(&pool, None)
}

#[get("/patient-info/{eye}")]
pub async fn aaaa_eye(
    pool: web::Data<DbPool>,
    path: web::Path<String>,
) -> Result<HttpResponse, Error> {
  aaaa_inner(&pool, Some(path.into_inner()))
}

async fn aaaa_inner(pool: &DbPool, path: Option<String>)

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@happybeing
Comment options

@robjtede
Comment options

Answer selected by qarmin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants