mirror of
https://github.com/vlang/v.git
synced 2025-09-19 04:17:46 -04:00
veb: add ctx.no_content()
+ prevent content-type being set if the mime type is empty (#23425)
This commit is contained in:
parent
36154b8631
commit
c92a21f8a5
@ -758,6 +758,8 @@ ctx.json(User{
|
|||||||
name: 'test'
|
name: 'test'
|
||||||
age: 20
|
age: 20
|
||||||
})
|
})
|
||||||
|
// send response HTTP_NO_CONTENT (204) without a content-type and body
|
||||||
|
ctx.no_content()
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Sending files
|
#### Sending files
|
||||||
|
@ -99,7 +99,9 @@ pub fn (mut ctx Context) send_response_to_client(mimetype string, response strin
|
|||||||
|
|
||||||
// set Content-Type and Content-Length headers
|
// set Content-Type and Content-Length headers
|
||||||
mut custom_mimetype := if ctx.content_type.len == 0 { mimetype } else { ctx.content_type }
|
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)
|
ctx.res.header.set(.content_type, custom_mimetype)
|
||||||
|
}
|
||||||
if ctx.res.body != '' {
|
if ctx.res.body != '' {
|
||||||
ctx.res.header.set(.content_length, ctx.res.body.len.str())
|
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)
|
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]
|
@[params]
|
||||||
pub struct RedirectParams {
|
pub struct RedirectParams {
|
||||||
pub:
|
pub:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user