diff --git a/vlib/veb/README.md b/vlib/veb/README.md index bb5f7e0f88..508bb1ebdb 100644 --- a/vlib/veb/README.md +++ b/vlib/veb/README.md @@ -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 diff --git a/vlib/veb/context.v b/vlib/veb/context.v index b88ceb18b6..a14aea5c64 100644 --- a/vlib/veb/context.v +++ b/vlib/veb/context.v @@ -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()) } @@ -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: