I want to post this data to Web API server:
public sealed class SomePostRequest
{
public int Id { get; set; }
public byte[] Content { get; set; }
}
Using this code for server:
[Route("Incoming")]
[ValidateModel]
public async Task<IHttpActionResult> PostIncomingData(SomePostRequest requestData)
{
// POST logic here
}
and this - for client:
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:25001/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{ "id", "1" },
{ "content", "123" }
});
var result = await client.PostAsync("api/SomeData/Incoming", content);
result.EnsureSuccessStatusCode();
everything works fine (at least, debugger stops at breakpoint in PostIncomingData
).
Since there is a byte
array, I don't want to serialize it as JSON, and want to post it as binary data to decrease network traffic (something like application/octet-stream
).
How this can be achieved?
I've tried to play with MultipartFormDataContent
, but looks like I just can't understand, how MultipartFormDataContent
will match signature of controller's method.
E.g., replacing content to this:
var content = new MultipartFormDataContent();
content.Add(new FormUrlEncodedContent(new Dictionary<string, string> { { "id", "1" } }));
var binaryContent = new ByteArrayContent(new byte[] { 1, 2, 3 });
binaryContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
content.Add(binaryContent, "content");
var result = await client.PostAsync("api/SomeData/Incoming", content);
result.EnsureSuccessStatusCode();
leads to error 415 ("Unsupported media type").
I convert
Byte Array
intoBase64 String
to post:and receiver can convert the
byte array
back toBase64 string
by:WebAPI v2.1 and beyond supports BSON (Binary JSON) out of the box, and even has a
MediaTypeFormatter
included for it. This means you can post your entire message in binary format.If you want to use it, you'll need to set it in
WebApiConfig
:Now, you an use the same
BsonMediaTypeFormatter
at the client side to serialize your request:Or, you can use Json.NET to serialize your class to BSON. Then, specify you want to use "application/bson" as your "Content-Type":
I have created this generic and cross platform method to support the BSON format using the Json.NET library so we can reuse it easier later. It works fine in Xamarin platform as well.
The method to help to serialise data to BSON format:
Then you can use the Post method like this:
Fyi, for protobuf serialization to request body posts