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

veb: add ctx.no_content() + prevent content-type being set if empty #23425

Merged
merged 2 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions vlib/veb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,8 @@ ctx.json(User{
name: 'test'
age: 20
})
// send response HTTP_NO_CONTENT (204) without a content-type and body
ctx.no_content()
```

#### Sending files
Expand Down
10 changes: 9 additions & 1 deletion vlib/veb/context.v
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, response strin

// set Content-Type and Content-Length headers
mut custom_mimetype := if ctx.content_type.len == 0 { mimetype } else { ctx.content_type }
ctx.res.header.set(.content_type, custom_mimetype)
if custom_mimetype != '' {
ctx.res.header.set(.content_type, custom_mimetype)
}
if ctx.res.body != '' {
ctx.res.header.set(.content_length, ctx.res.body.len.str())
}
Expand Down Expand Up @@ -225,6 +227,12 @@ pub fn (mut ctx Context) server_error(msg string) Result {
return ctx.send_response_to_client('text/plain', msg)
}

// send a 204 No Content response without body and content-type
pub fn (mut ctx Context) no_content() Result {
ctx.res.set_status(.no_content)
return ctx.send_response_to_client('', '')
}

@[params]
pub struct RedirectParams {
pub:
Expand Down
Loading