Skip to content

Commit c92a21f

Browse files
authored
veb: add ctx.no_content() + prevent content-type being set if the mime type is empty (#23425)
1 parent 36154b8 commit c92a21f

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎vlib/veb/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,8 @@ ctx.json(User{
758758
name: 'test'
759759
age: 20
760760
})
761+
// send response HTTP_NO_CONTENT (204) without a content-type and body
762+
ctx.no_content()
761763
```
762764

763765
#### Sending files

‎vlib/veb/context.v‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, response strin
9999

100100
// set Content-Type and Content-Length headers
101101
mut custom_mimetype := if ctx.content_type.len == 0 { mimetype } else { ctx.content_type }
102-
ctx.res.header.set(.content_type, custom_mimetype)
102+
if custom_mimetype != '' {
103+
ctx.res.header.set(.content_type, custom_mimetype)
104+
}
103105
if ctx.res.body != '' {
104106
ctx.res.header.set(.content_length, ctx.res.body.len.str())
105107
}
@@ -225,6 +227,12 @@ pub fn (mut ctx Context) server_error(msg string) Result {
225227
return ctx.send_response_to_client('text/plain', msg)
226228
}
227229

230+
// send a 204 No Content response without body and content-type
231+
pub fn (mut ctx Context) no_content() Result {
232+
ctx.res.set_status(.no_content)
233+
return ctx.send_response_to_client('', '')
234+
}
235+
228236
@[params]
229237
pub struct RedirectParams {
230238
pub:

0 commit comments

Comments
 (0)