From edfab5db790822798f05728b4bd33bece1f4b91b Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 11 Mar 2026 12:48:57 +0300 Subject: [PATCH] veb: context.set_header not working as expected for .content-type (fixes #24016) --- vlib/veb/context.v | 7 +++++-- .../veb/tests/content_type_header_regression_test.v | 13 +++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 vlib/veb/tests/content_type_header_regression_test.v diff --git a/vlib/veb/context.v b/vlib/veb/context.v index 2b3e9266e..c9debdbc7 100644 --- a/vlib/veb/context.v +++ b/vlib/veb/context.v @@ -112,8 +112,11 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, response strin } $else { ctx.res.body = response.clone() } - // set Content-Type and Content-Length headers - mut custom_mimetype := if ctx.content_type.len == 0 { mimetype } else { ctx.content_type } + // Prefer explicit overrides from Context state or a pre-set response header. + mut custom_mimetype := ctx.content_type + if custom_mimetype.len == 0 { + custom_mimetype = ctx.res.header.get(.content_type) or { mimetype } + } if custom_mimetype != '' { ctx.res.header.set(.content_type, custom_mimetype) } diff --git a/vlib/veb/tests/content_type_header_regression_test.v b/vlib/veb/tests/content_type_header_regression_test.v new file mode 100644 index 000000000..24297c341 --- /dev/null +++ b/vlib/veb/tests/content_type_header_regression_test.v @@ -0,0 +1,13 @@ +import net.http +import veb + +fn test_set_header_content_type_preserves_charset_parameter() { + mut ctx := veb.Context{ + res: http.Response{} + } + ctx.set_header(.content_type, 'text/html; charset=utf-8') + + ctx.text('Hello, World!') + + assert ctx.res.header.get(.content_type)! == 'text/html; charset=utf-8' +} -- 2.39.5