diff --git a/vlib/vweb/tests/vweb_test.v b/vlib/vweb/tests/vweb_test.v index bdb05d9a3600f7..7902977c1d88f1 100644 --- a/vlib/vweb/tests/vweb_test.v +++ b/vlib/vweb/tests/vweb_test.v @@ -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: { diff --git a/vlib/vweb/tests/vweb_test_server/server.v b/vlib/vweb/tests/vweb_test_server/server.v index 28573318cabf66..2768b3bf73c0cb 100644 --- a/vlib/vweb/tests/vweb_test_server/server.v +++ b/vlib/vweb/tests/vweb_test_server/server.v @@ -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}') diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 0434344b8b1a9b..5d08d80ea723df 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -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)