11using Newtonsoft . Json . Linq ;
22using System ;
33using System . Collections . Generic ;
4+ using System . IO ;
45using System . Linq ;
56using System . Net ;
67using System . Net . Http ;
8+ using System . Net . Http . Headers ;
79using System . Text ;
810using System . Threading . Tasks ;
911
@@ -193,10 +195,8 @@ public async Task<Response<T>> DeleteAsync<T>(string service)
193195 public async Task < Response < T > > PostAsync < T > ( string service , object value )
194196 {
195197 var uri = new Uri ( BaseUri , service ) ;
196-
197198 using var msg = new HttpRequestMessage ( HttpMethod . Post , uri ) ;
198- msg . Content = buildJsonContent ( value , msg ) ;
199-
199+ msg . Content = buildObjectContent ( value , msg ) ;
200200 return await sendMessageAsync < T > ( uri , msg ) ;
201201 }
202202 /// <summary>
@@ -219,7 +219,7 @@ public async Task<Response> PostAsync(string service, object value)
219219 {
220220 var uri = new Uri ( BaseUri , service ) ;
221221 using var msg = new HttpRequestMessage ( HttpMethod . Post , uri ) ;
222- msg . Content = buildJsonContent ( value , msg ) ;
222+ msg . Content = buildObjectContent ( value , msg ) ;
223223 return await sendMessageAsync ( msg ) ;
224224 }
225225
@@ -237,6 +237,19 @@ public async Task<Response<T>> PostAsync<T>(string service, HttpContent content)
237237
238238 return await sendMessageAsync < T > ( uri , msg ) ;
239239 }
240+ /// <summary>
241+ /// Sends a Post request with specified content and process the returned content
242+ /// </summary>
243+ /// <param name="service">Service to request from, will be concatenated with BaseUri</param>
244+ /// <param name="content">Content to be sent</param>
245+ public async Task < Response > PostAsync ( string service , HttpContent content )
246+ {
247+ var uri = new Uri ( BaseUri , service ) ;
248+ using var msg = new HttpRequestMessage ( HttpMethod . Post , uri ) ;
249+ if ( content is not null ) msg . Content = content ;
250+
251+ return await sendMessageAsync ( msg ) ;
252+ }
240253
241254 /* PUT */
242255 /// <summary>
@@ -362,6 +375,25 @@ private async Task<Response<T>> sendMessageAsync<T>(Uri uri, HttpRequestMessage
362375 }
363376 }
364377
378+ private HttpContent buildObjectContent ( object value , HttpRequestMessage msg )
379+ {
380+ if ( value is byte [ ] bVal )
381+ {
382+ var content = new ByteArrayContent ( bVal ) ;
383+ content . Headers . ContentType = new MediaTypeHeaderValue ( "application/octet-stream" ) ;
384+ return content ;
385+ }
386+ else if ( value is Stream stream )
387+ {
388+ var content = new StreamContent ( stream ) ;
389+ content . Headers . ContentType = new MediaTypeHeaderValue ( "application/octet-stream" ) ;
390+ return content ;
391+ }
392+ else
393+ {
394+ return buildJsonContent ( value , msg ) ;
395+ }
396+ }
365397 private HttpContent buildJsonContent ( object value , HttpRequestMessage msg )
366398 {
367399 var jsonValue = Newtonsoft . Json . JsonConvert . SerializeObject ( value , new Newtonsoft . Json . JsonSerializerSettings ( )
0 commit comments