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

This commit is contained in:
Louis Brauer 2025-01-11 10:39:50 +01:00 committed by GitHub
parent 36154b8631
commit c92a21f8a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 1 deletions

View File

@ -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

View File

@ -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 }
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())
}
@ -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: