I am not sure that this is what you are asking for but I think it might be. I have found as long as I can get something into and out of binary, then I am golden. This also provides me with good interoperability with other languages like C+++.

If not what you are asking.. tell me and I will delete my answer.

This is what I did. I specifically needed binary data to be passed to my controller and binary data returned.

For my use case, the secret was in application/octet-stream.

[Route("api/[controller]")]
[ApiController]
public class SomeDangController : ControllerBase
{
    [HttpPost]
    [Produces("application/octet-stream")]
    public async Task<Stream> GetBinaryThingy()
    {
        using (var reader = new StreamReader(HttpContext.Request.Body))
        {
            using (MemoryStream ms = new MemoryStream())
            {
                await reader.BaseStream.CopyToAsync(ms);

                var buffer = ms.ToArray();
                //buffer now holds binary data from the client
            }
       }
       byte[] bytes_return = null  //populate your binary return here.
       return new MemoryStream(bytes_return);
   }
}

And on the client.. something like this:

    public async System.Threading.Tasks.Task<byte[]> PostBinaryStuffAsync(byte[] buffer)
    {
        ByteArrayContent byteContent = new ByteArrayContent(buffer);

        var response = await client.PostAsync(dangServerURL, byteContent);

        var responseBytes = await response.Content.ReadAsByteArrayAsync();

        return responseBytes;
    }

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top