Added ReadAsForm to the RequestExtensions to help with reading form data. Bumped package verison to 1.6.0
This commit is contained in:
		| @@ -82,7 +82,8 @@ namespace MontoyaTech.Rest.Net.Example | ||||
|                 new Route(HttpRequestMethod.Get, "/auth/", Json), | ||||
|                 new Route(HttpRequestMethod.Get, "/auth/role", GetRole), | ||||
|                 new Route(HttpRequestMethod.Post, "/upload", Upload), | ||||
|                 new Route(HttpRequestMethod.Get, "/download", Download) | ||||
|                 new Route(HttpRequestMethod.Get, "/download", Download), | ||||
|                 new Route(HttpRequestMethod.Post, "/form", FormTest) | ||||
|             ); | ||||
|  | ||||
|             string code = listener.GenerateCSharpClient(); | ||||
| @@ -224,5 +225,12 @@ namespace MontoyaTech.Rest.Net.Example | ||||
|         { | ||||
|             return context.Response.WithStatus(HttpStatusCode.OK).WithText("Hello world"); | ||||
|         } | ||||
|  | ||||
|         [RouteGroup("Form")] | ||||
|         [RouteResponse(typeof(Dictionary<string, string>))] | ||||
|         public static HttpListenerResponse FormTest(HttpListenerContext context) | ||||
|         { | ||||
|             return context.Response.WithStatus(HttpStatusCode.OK).WithJson(context.Request.ReadAsForm()); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -97,5 +97,24 @@ namespace MontoyaTech.Rest.Net | ||||
|                 return false; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Reads the content of a HttpListenerRequest as http form data and returns it as a dictionary. Any duplicate keys are ignored. | ||||
|         /// </summary> | ||||
|         /// <param name="request"></param> | ||||
|         /// <returns>A dictionary of keyvalue pairs from the form data.</returns> | ||||
|         public static Dictionary<string, string> ReadAsForm(this HttpListenerRequest request) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 using (var input = request.InputStream) | ||||
|                 using (var stream = new StreamReader(input)) | ||||
|                     return stream.ReadToEnd().Trim().Split('&').Where(value => value.Contains('=')).ToLookup(value => value.Split('=')[0].Trim(), value => value.Split('=')[1].Trim()).ToDictionary(group => group.Key, group => group.FirstOrDefault()); | ||||
|             } | ||||
|             catch | ||||
|             { | ||||
|                 return new Dictionary<string, string>(); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -17,7 +17,7 @@ | ||||
|     <AssemblyName>MontoyaTech.Rest.Net</AssemblyName> | ||||
|     <RootNamespace>MontoyaTech.Rest.Net</RootNamespace> | ||||
|     <GenerateDocumentationFile>True</GenerateDocumentationFile> | ||||
|     <Version>1.5.9</Version> | ||||
|     <Version>1.6.0</Version> | ||||
|     <PackageReleaseNotes></PackageReleaseNotes> | ||||
|     <PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon> | ||||
|   </PropertyGroup> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user