Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A route producing the Unit type () should generate a HTTP 204 response #2363

Closed
1 task done
jbethune opened this issue Nov 27, 2023 · 2 comments
Closed
1 task done
Labels
A-axum-core C-enhancement Category: A PR with an enhancement

Comments

@jbethune
Copy link

  • I have looked for existing issues (including closed) about this

Feature Request

This is not something super important, but I want to describe a potential semantic improvement on how axum uses HTTP.

Axum currently returns HTTP status 200 for a route that returns the unit type:

async fn my_route() -> () { // explicit return type for clarity
    println!("I am not returning anything");
}

But a HTTP 204 status code (=no content) would be more precise, because the unit type is essentially an empty value.

Motivation

This is mostly useful for routes that get called via ajax. By being more explicit, API breakage can be detected more easily and ajax handling logic may branch on the status code.

Somewhat related: Option<T> return types could also benefit from a 200/204 distinction.

Proposal

My understanding is that this part has to be changed:

impl IntoResponse for () {
fn into_response(self) -> Response {
Body::empty().into_response()
}
}

This could be as simple as creating the response like above, but before returning, update the status code to 204.

Alternatives

Just leave things as they are. Which would still be fine.

@davidpdrsn davidpdrsn added C-enhancement Category: A PR with an enhancement A-axum-core labels Nov 27, 2023
@davidpdrsn
Copy link
Member

I suspect that might be difficult because of how we implement IntoResponse for tuples but worth a shot! I agree it makes sense.

@davidpdrsn
Copy link
Member

Gonna close this. See #2393 (comment) for why.

@davidpdrsn davidpdrsn closed this as not planned Won't fix, can't repro, duplicate, stale Dec 29, 2023
oxalica added a commit to oxalica/axum that referenced this issue Oct 11, 2024
We cannot change the behavior of `<() as IntoResponse>::into_response`,
so add a newtype struct to explicitly use 204 No Content status.

Refs: tokio-rs#2363, tokio-rs#2393
oxalica added a commit to oxalica/axum that referenced this issue Oct 11, 2024
We cannot change the behavior of `<() as IntoResponse>::into_response`,
so add a newtype struct to explicitly use 204 No Content status.

Refs: tokio-rs#2363, tokio-rs#2393
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-axum-core C-enhancement Category: A PR with an enhancement
Projects
None yet
Development

No branches or pull requests

2 participants