Skip to content

Commit d319843

Browse files
Support Post types byte[] and Stream
1 parent 1f98aa8 commit d319843

1 file changed

Lines changed: 36 additions & 4 deletions

File tree

Simple.API/ClientInfo.cs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using Newtonsoft.Json.Linq;
22
using System;
33
using System.Collections.Generic;
4+
using System.IO;
45
using System.Linq;
56
using System.Net;
67
using System.Net.Http;
8+
using System.Net.Http.Headers;
79
using System.Text;
810
using 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

Comments
 (0)