@@ -9,6 +9,7 @@ namespace FrApp42.Web.API
99 public class Request
1010 {
1111 #region Variables
12+
1213 /// <summary>
1314 /// The HTTP client used to send requests.
1415 /// </summary>
@@ -61,12 +62,13 @@ public class Request
6162 /// <summary>
6263 /// Gets the name of the binary document file.
6364 /// </summary>
64- public string DocumentFileName { get ; private set ; } = null ;
65+ public string ? DocumentFileName { get ; private set ; } = null ;
6566
6667 /// <summary>
6768 /// Gets the content type of the request.
6869 /// </summary>
6970 public string ContentType { get ; private set ; } = null ;
71+
7072 #endregion
7173
7274 #region Constructors
@@ -202,12 +204,12 @@ public Request AddJsonBody(object body)
202204 }
203205
204206 /// <summary>
205- /// Adds a binary document to the request content.
207+ /// Adds a <see cref="byte[]"/> to the request content.
206208 /// </summary>
207209 /// <param name="document">The binary file content.</param>
208210 /// <param name="fileName">The name of the file.</param>
209211 /// <returns>Instance</returns>
210- public Request AddDocumentBody ( byte [ ] document , string fileName )
212+ public Request AddByteBody ( byte [ ] document , string ? fileName )
211213 {
212214 DocumentBody = document ;
213215 DocumentFileName = fileName ;
@@ -325,9 +327,38 @@ public async Task<Result<byte[]>> RunGetBytes()
325327 return result ;
326328 }
327329
330+ /// <summary>
331+ /// Executes the HTTP request by sending the raw binary content directly in the request body,
332+ /// without using multipart encoding. This method is ideal for APIs expecting direct binary content,
333+ /// equivalent to Postman's "Binary" body type.
334+ /// </summary>
335+ /// <typeparam name="T">The type of the expected response.</typeparam>
336+ /// <returns>
337+ /// A <see cref="Result{T}"/> object containing the deserialized response, the HTTP status code,
338+ /// and any error message if the request fails.
339+ /// </returns>
340+ public async Task < Result < T > > RunBinaryRaw < T > ( )
341+ {
342+ HttpRequestMessage request = BuildBaseRequest ( ) ;
343+
344+ if ( DocumentBody == null )
345+ {
346+ return new Result < T >
347+ {
348+ Error = "Document cannot be null" ,
349+ StatusCode = 500
350+ } ;
351+ }
352+
353+ request . Content = new ByteArrayContent ( DocumentBody ) ;
354+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( ContentType ?? "application/octet-stream" ) ;
355+
356+ return await Process < T > ( request ) ;
357+ }
358+
328359 #endregion
329360
330- #region Private function
361+ #region Private methods
331362
332363 /// <summary>
333364 /// Constructs the URL for the request, including any query parameters if present.
@@ -403,19 +434,19 @@ private async Task<Result<T>> Process<T>(HttpRequestMessage request)
403434
404435 if ( response . IsSuccessStatusCode )
405436 {
406- string ? MediaType = response . Content ? . Headers ? . ContentType ? . MediaType . ToLower ( ) ;
407- string ContentResponse = await response . Content ? . ReadAsStringAsync ( ) ;
437+ string ? mediaType = response . Content ? . Headers ? . ContentType ? . MediaType . ToLower ( ) ;
438+ string contentResponse = await response . Content ? . ReadAsStringAsync ( ) ;
408439
409440 switch ( true )
410441 {
411- case bool b when ( MediaType . Contains ( "xml" ) ) :
442+ case bool b when ( mediaType . Contains ( "xml" ) ) :
412443 XmlSerializer xmlSerializer = new ( typeof ( T ) ) ;
413- StringReader reader = new ( ContentResponse ) ;
444+ StringReader reader = new ( contentResponse ) ;
414445
415446 result . Value = ( T ) xmlSerializer . Deserialize ( reader ) ;
416447 break ;
417- case bool b when ( MediaType . Contains ( "application/json" ) ) :
418- result . Value = JsonConvert . DeserializeObject < T > ( ContentResponse ) ;
448+ case bool b when ( mediaType . Contains ( "application/json" ) ) :
449+ result . Value = JsonConvert . DeserializeObject < T > ( contentResponse ) ;
419450 break ;
420451 default :
421452 result . Value = default ;
0 commit comments