From cc1deb27a14e431eb2ffbadae138a1de90679221 Mon Sep 17 00:00:00 2001 From: Sik Date: Fri, 28 Mar 2025 14:09:51 +0100 Subject: [PATCH 1/2] fix(serialize): replaced 'JsonPropertyName' to 'JsonProperty' attribute --- Web.Test/Models/HttpBinPostResponse.cs | 18 +++++------ Web.Test/Models/HttpBinResponseBase.cs | 44 +++++++++++++------------- Web/README.md | 6 ++-- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/Web.Test/Models/HttpBinPostResponse.cs b/Web.Test/Models/HttpBinPostResponse.cs index fd0ece0..88641ae 100644 --- a/Web.Test/Models/HttpBinPostResponse.cs +++ b/Web.Test/Models/HttpBinPostResponse.cs @@ -1,19 +1,19 @@ -using System.Text.Json.Serialization; +using Newtonsoft.Json; namespace Web.Test.Models { public class HttpBinPostResponse : HttpBinResponseBase { - [JsonPropertyName("data")] - public string Data { get; set; } + [JsonProperty("data")] + public string Data { get; set; } = string.Empty; - [JsonPropertyName("files")] - public Dictionary Files { get; set; } + [JsonProperty("files")] + public Dictionary Files { get; set; } = []; - [JsonPropertyName("form")] - public Dictionary Form { get; set; } + [JsonProperty("form")] + public Dictionary Form { get; set; } = []; - [JsonPropertyName("json")] - public object Json { get; set; } + [JsonProperty("json")] + public object Json { get; set; } = new(); } } diff --git a/Web.Test/Models/HttpBinResponseBase.cs b/Web.Test/Models/HttpBinResponseBase.cs index 2f930af..f4a1e72 100644 --- a/Web.Test/Models/HttpBinResponseBase.cs +++ b/Web.Test/Models/HttpBinResponseBase.cs @@ -1,40 +1,40 @@ -using System.Text.Json.Serialization; +using Newtonsoft.Json; namespace Web.Test.Models { public class HttpBinResponseBase { - [JsonPropertyName("args")] - public Dictionary Args { get; set; } + [JsonProperty("args")] + public Dictionary Args { get; set; } = []; - [JsonPropertyName("headers")] - public HttpBinResponseHeaders Headers { get; set; } + [JsonProperty("headers")] + public HttpBinResponseHeaders Headers { get; set; } = new(); - [JsonPropertyName("origin")] - public string Origin { get; set; } + [JsonProperty("origin")] + public string Origin { get; set; } = string.Empty; - [JsonPropertyName("url")] - public string Url { get; set; } + [JsonProperty("url")] + public string Url { get; set; } = string.Empty; } public class HttpBinResponseHeaders { - [JsonPropertyName("Accept")] - public string Accept { get; set; } + [JsonProperty("Accept")] + public string Accept { get; set; } = string.Empty; - [JsonPropertyName("Accept-Encoding")] - public string AcceptEncoding { get; set; } + [JsonProperty("Accept-Encoding")] + public string AcceptEncoding { get; set; } = string.Empty; - [JsonPropertyName("Accept-Language")] - public string AcceptLanguage { get; set; } + [JsonProperty("Accept-Language")] + public string AcceptLanguage { get; set; } = string.Empty; - [JsonPropertyName("Host")] - public string Host { get; set; } + [JsonProperty("Host")] + public string Host { get; set; } = string.Empty; - [JsonPropertyName("User-Agent")] - public string UserAgent { get; set; } + [JsonProperty("User-Agent")] + public string UserAgent { get; set; } = string.Empty; - [JsonPropertyName("X-Amzn-Trace-Id")] - public string AmazonTraceId { get; set; } - } + [JsonProperty("X-Amzn-Trace-Id")] + public string AmazonTraceId { get; set; } = string.Empty; + } } diff --git a/Web/README.md b/Web/README.md index f452943..7da3d15 100644 --- a/Web/README.md +++ b/Web/README.md @@ -15,7 +15,7 @@ Install-Package FrApp42.Net ```csharp using FrApp42.Web.API; -using System.Text.Json.Serialization; +using Newtonsoft.Json; string url = "your-url"; @@ -34,10 +34,10 @@ if (result.StatusCode == 200 && result.Value != null) class MyModel { - [JsonPropertyName("name")] + [JsonProperty("name")] public string Name { get; set; } - [JsonPropertyName("description")] + [JsonProperty("description")] public string Description { get; set; } } ``` From 16fc8404577e1fc87f7df41f50eefbaba3248c33 Mon Sep 17 00:00:00 2001 From: Sik Date: Fri, 28 Mar 2025 14:10:24 +0100 Subject: [PATCH 2/2] feat(web): added method to post binary byte[] --- Web.Test/RequestTest.cs | 6 ++--- Web/API/Request.cs | 51 +++++++++++++++++++++++++++++++++-------- Web/Web.csproj | 15 +++++------- 3 files changed, 50 insertions(+), 22 deletions(-) diff --git a/Web.Test/RequestTest.cs b/Web.Test/RequestTest.cs index ae6877b..c89c39b 100644 --- a/Web.Test/RequestTest.cs +++ b/Web.Test/RequestTest.cs @@ -72,12 +72,12 @@ public async Task SendPostRequestWithFile() Request request = new(TestPostUrl, HttpMethod.Post); request .SetContentType("text/plain") - .AddDocumentBody(fileBytes, fileName); + .AddByteBody(fileBytes, fileName); Result result = await request.RunDocument(); AssertResponse(result, TestPostUrl); - StringAssert.Contains(result.Value.Data, "Hello world", "File content should contain 'Hello World'"); + StringAssert.Contains(result.Value?.Data, "Hello world", "File content should contain 'Hello World'"); } [TestMethod] @@ -135,5 +135,5 @@ private void AssertImageResponse(Result result, string imgType) Assert.IsTrue(result.Value.Length > 0, "Image data should not be empty"); File.WriteAllBytes($"test_image.{imgType}", result.Value); } - } + } } diff --git a/Web/API/Request.cs b/Web/API/Request.cs index b695822..f1356af 100644 --- a/Web/API/Request.cs +++ b/Web/API/Request.cs @@ -9,6 +9,7 @@ namespace FrApp42.Web.API public class Request { #region Variables + /// /// The HTTP client used to send requests. /// @@ -61,12 +62,13 @@ public class Request /// /// Gets the name of the binary document file. /// - public string DocumentFileName { get; private set; } = null; + public string? DocumentFileName { get; private set; } = null; /// /// Gets the content type of the request. /// public string ContentType { get; private set; } = null; + #endregion #region Constructors @@ -202,12 +204,12 @@ public Request AddJsonBody(object body) } /// - /// Adds a binary document to the request content. + /// Adds a to the request content. /// /// The binary file content. /// The name of the file. /// Instance - public Request AddDocumentBody(byte[] document, string fileName) + public Request AddByteBody(byte[] document, string? fileName) { DocumentBody = document; DocumentFileName = fileName; @@ -325,9 +327,38 @@ public async Task> RunGetBytes() return result; } + /// + /// Executes the HTTP request by sending the raw binary content directly in the request body, + /// without using multipart encoding. This method is ideal for APIs expecting direct binary content, + /// equivalent to Postman's "Binary" body type. + /// + /// The type of the expected response. + /// + /// A object containing the deserialized response, the HTTP status code, + /// and any error message if the request fails. + /// + public async Task> RunBinaryRaw() + { + HttpRequestMessage request = BuildBaseRequest(); + + if (DocumentBody == null) + { + return new Result + { + Error = "Document cannot be null", + StatusCode = 500 + }; + } + + request.Content = new ByteArrayContent(DocumentBody); + request.Content.Headers.ContentType = new MediaTypeHeaderValue(ContentType ?? "application/octet-stream"); + + return await Process(request); + } + #endregion - #region Private function + #region Private methods /// /// Constructs the URL for the request, including any query parameters if present. @@ -403,19 +434,19 @@ private async Task> Process(HttpRequestMessage request) if (response.IsSuccessStatusCode) { - string? MediaType = response.Content?.Headers?.ContentType?.MediaType.ToLower(); - string ContentResponse = await response.Content?.ReadAsStringAsync(); + string? mediaType = response.Content?.Headers?.ContentType?.MediaType.ToLower(); + string contentResponse = await response.Content?.ReadAsStringAsync(); switch (true) { - case bool b when (MediaType.Contains("xml")): + case bool b when (mediaType.Contains("xml")): XmlSerializer xmlSerializer = new(typeof(T)); - StringReader reader = new(ContentResponse); + StringReader reader = new(contentResponse); result.Value = (T)xmlSerializer.Deserialize(reader); break; - case bool b when (MediaType.Contains("application/json")): - result.Value = JsonConvert.DeserializeObject(ContentResponse); + case bool b when (mediaType.Contains("application/json")): + result.Value = JsonConvert.DeserializeObject(contentResponse); break; default: result.Value = default; diff --git a/Web/Web.csproj b/Web/Web.csproj index 855c4d7..8f87d55 100644 --- a/Web/Web.csproj +++ b/Web/Web.csproj @@ -1,7 +1,7 @@  - net8.0 + net8.0 enable enable FrApp42.$(MSBuildProjectName) @@ -20,10 +20,14 @@ README.md FrenchyApps42 logo.png - 1.2.2 + 1.3.0 + + \ + True + True \ @@ -38,11 +42,4 @@ - - - \ - True - - -