@@ -9,6 +9,7 @@ namespace FrApp42.Web.API
9
9
public class Request
10
10
{
11
11
#region Variables
12
+
12
13
/// <summary>
13
14
/// The HTTP client used to send requests.
14
15
/// </summary>
@@ -61,12 +62,13 @@ public class Request
61
62
/// <summary>
62
63
/// Gets the name of the binary document file.
63
64
/// </summary>
64
- public string DocumentFileName { get ; private set ; } = null ;
65
+ public string ? DocumentFileName { get ; private set ; } = null ;
65
66
66
67
/// <summary>
67
68
/// Gets the content type of the request.
68
69
/// </summary>
69
70
public string ContentType { get ; private set ; } = null ;
71
+
70
72
#endregion
71
73
72
74
#region Constructors
@@ -202,12 +204,12 @@ public Request AddJsonBody(object body)
202
204
}
203
205
204
206
/// <summary>
205
- /// Adds a binary document to the request content.
207
+ /// Adds a <see cref="byte[]"/> to the request content.
206
208
/// </summary>
207
209
/// <param name="document">The binary file content.</param>
208
210
/// <param name="fileName">The name of the file.</param>
209
211
/// <returns>Instance</returns>
210
- public Request AddDocumentBody ( byte [ ] document , string fileName )
212
+ public Request AddByteBody ( byte [ ] document , string ? fileName )
211
213
{
212
214
DocumentBody = document ;
213
215
DocumentFileName = fileName ;
@@ -325,9 +327,38 @@ public async Task<Result<byte[]>> RunGetBytes()
325
327
return result ;
326
328
}
327
329
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
+
328
359
#endregion
329
360
330
- #region Private function
361
+ #region Private methods
331
362
332
363
/// <summary>
333
364
/// 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)
403
434
404
435
if ( response . IsSuccessStatusCode )
405
436
{
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 ( ) ;
408
439
409
440
switch ( true )
410
441
{
411
- case bool b when ( MediaType . Contains ( "xml" ) ) :
442
+ case bool b when ( mediaType . Contains ( "xml" ) ) :
412
443
XmlSerializer xmlSerializer = new ( typeof ( T ) ) ;
413
- StringReader reader = new ( ContentResponse ) ;
444
+ StringReader reader = new ( contentResponse ) ;
414
445
415
446
result . Value = ( T ) xmlSerializer . Deserialize ( reader ) ;
416
447
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 ) ;
419
450
break ;
420
451
default :
421
452
result . Value = default ;
0 commit comments