From 41bbabf1cb6691acd52d9347af273225ab4a618e Mon Sep 17 00:00:00 2001 From: MattMo Date: Sun, 6 Feb 2022 21:32:01 -0800 Subject: [PATCH] Added more examples. WithText and WithJson now specify Utf16 to support rich content. --- Rest.Net.Example/Rest.Net.Example/Program.cs | 20 ++++++++++++------- .../HttpListenerResponseExtensions.cs | 4 ++-- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Rest.Net.Example/Rest.Net.Example/Program.cs b/Rest.Net.Example/Rest.Net.Example/Program.cs index cf482e7..aecad43 100644 --- a/Rest.Net.Example/Rest.Net.Example/Program.cs +++ b/Rest.Net.Example/Rest.Net.Example/Program.cs @@ -30,7 +30,8 @@ namespace MontoyaTech.Rest.Net.Example var listener = new RouteListener(8080, new Route(HttpRequestMethod.Get, "/status", Status), new Route(HttpRequestMethod.Post, "/add/{a}/{b}", Add), - new Route(HttpRequestMethod.Post, "/signup/{username}", Signup) + new Route(HttpRequestMethod.Post, "/signup/{username}", Signup), + new Route(HttpRequestMethod.Get, "/json", Json) ); listener.Start(); @@ -40,19 +41,24 @@ namespace MontoyaTech.Rest.Net.Example listener.Block(); } - public static HttpListenerResponse Status(HttpListenerRequest request, HttpListenerResponse response) + public static HttpListenerResponse Status(RouteContext context) { - return response.WithStatus(HttpStatusCode.OK).WithText("Everything is operational."); + return context.Response.WithStatus(HttpStatusCode.OK).WithText("Everything is operational. 👍"); } - public static HttpListenerResponse Add(HttpListenerRequest request, HttpListenerResponse response, double a, double b) + public static HttpListenerResponse Add(RouteContext context, double a, double b) { - return response.WithStatus(HttpStatusCode.OK).WithText((a + b).ToString()); + return context.Response.WithStatus(HttpStatusCode.OK).WithText((a + b).ToString()); } - public static HttpListenerResponse Signup(HttpListenerRequest request, HttpListenerResponse response, User user) + public static HttpListenerResponse Signup(RouteContext context, User user) { - return response.WithStatus(HttpStatusCode.OK).WithText("User:" + user.Name); + return context.Response.WithStatus(HttpStatusCode.OK).WithText("User:" + user.Name); + } + + public static HttpListenerResponse Json(RouteContext context) + { + return context.Response.WithStatus(HttpStatusCode.OK).WithJson(new User("Rest.Net")); } } } diff --git a/Rest.Net/Rest.Net/Extensions/HttpListenerResponseExtensions.cs b/Rest.Net/Rest.Net/Extensions/HttpListenerResponseExtensions.cs index 9063e35..c856ed0 100644 --- a/Rest.Net/Rest.Net/Extensions/HttpListenerResponseExtensions.cs +++ b/Rest.Net/Rest.Net/Extensions/HttpListenerResponseExtensions.cs @@ -22,7 +22,7 @@ namespace MontoyaTech.Rest.Net /// public static HttpListenerResponse WithText(this HttpListenerResponse response, string text) { - response.ContentType = "text/plain"; + response.ContentType = "text/plain; charset=utf-16"; var bytes = Encoding.Unicode.GetBytes(text); response.OutputStream.Write(bytes, 0, bytes.Length); @@ -38,7 +38,7 @@ namespace MontoyaTech.Rest.Net /// public static HttpListenerResponse WithJson(this HttpListenerResponse response, object obj) { - response.ContentType = "application/json"; + response.ContentType = "application/json; charset=utf-16"; var bytes = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(obj)); response.OutputStream.Write(bytes, 0, bytes.Length);