Skip to content

Commit

Permalink
veb: add ctx.no_content() + prevent content-type being set if empty
Browse files Browse the repository at this point in the history
  • Loading branch information
louis77 committed Jan 10, 2025
1 parent 36154b8 commit f85c2ea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
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.len != '' {
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

0 comments on commit f85c2ea

Please sign in to comment.