Skip to content

Commit

Permalink
vweb: add json_raw
Browse files Browse the repository at this point in the history
  • Loading branch information
esquerbatua committed Aug 29, 2024
1 parent 01fd719 commit bc1cbd9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vlib/vweb/tests/vweb_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ fn test_http_client_json_post() {
assert '${ouser}' == '${nuser2}'
}

fn test_http_client_json_raw_get() {
mut res := http.get('http://${localserver}/json_raw') or { panic(err) }
assert res.header.get(.content_type)! == 'application/json'
assert res.body == '{"foo": "bar"}'
}

fn test_http_client_multipart_form_data() {
mut form_config := http.PostMultipartFormConfig{
form: {
Expand Down
6 changes: 6 additions & 0 deletions vlib/vweb/tests/vweb_test_server/server.v
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ pub fn (mut app App) json() vweb.Result {
return app.ok(app.req.data)
}

// Make sure [get] works without the path, and works with json_raw
@['json_raw'; get]
pub fn (mut app App) raw_json() vweb.Result {
return app.json_raw('{"foo": "bar"}')
}

// Custom 404 page
pub fn (mut app App) not_found() vweb.Result {
linfo('>>>>> ${@LOCATION}')
Expand Down
6 changes: 6 additions & 0 deletions vlib/vweb/vweb.v
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ pub fn (mut ctx Context) text(s string) Result {
return Result{}
}

// Response with content as payload and content-type `application/json`
pub fn (mut ctx Context) json_raw(content string) Result {
ctx.send_response_to_client('application/json', content)
return Result{}
}

// Response with json_s as payload and content-type `application/json`
pub fn (mut ctx Context) json[T](j T) Result {
json_s := json.encode(j)
Expand Down

0 comments on commit bc1cbd9

Please sign in to comment.