1

I have two byte array in the angularjs application. I want to pass two byte array to api end point in the body as a post operation.

       function getPayload(byteArray1, byteArray2) {
                return {
                    "byteArrayInfo1": byteArray1,
                    "byteArrayInfo2": byteArray2
                };
            }

        var executePostRequest = function (command, byteArray1, byteArray2) {
                    return $http({
                        method: 'POST',
                        headers: { 'Content-Type': 'application/octet-stream' },
                        data: getPayload(byteArray1,byteArray2),
                        url: env.endPointBasePath + command
                    });
            };

When i am debugging to dot net api end point, it is not getting two byte array. I tried changing content-type to 'Content-Type': 'application/json'. It did not work.

My concern here is now, is it good to try to pass two byte array in one post call? Is it good practice? I would be happy if there is technique in javascript/angularjs to merge two byte arrays and split in app api end point.

App api end point is self hoisted owin based appliation. I was trying to retrive the body from context request body and map it to ByteArrayPayload class.

private static ByteArrayPayload ExtractContent(OwinContext context)
    {
        if (ContentLengthIsNullOrZero(context.Request.Headers.ContentLength))
        {
            return null;
        }

        int contentLength = Int32.Parse(context.Request.Headers.ContentLength);
        var content = context.Request.Body.Read(contentLength);
       //map content to ByteArrayPayload 
       return new ByteArrayPayload ();
    }

  public class ByteArrayPayload{
    public byte[] ByteArrayInfo1 {get;set;}
    public byte[] ByteArrayInfo2 {get;set;}
  }
4
  • Will you post your controller code too Commented Dec 13, 2017 at 22:16
  • When you say "byte array", do you mean an ArrayBuffer object? Commented Dec 13, 2017 at 23:06
  • The best way to send binary content is by using an ArrayBufferView or Blobs. The FormData API can be used to send multiple binary items but is inefficient because the base64 encoding adds 33% extra overhead. Commented Dec 13, 2017 at 23:18
  • I updated my app api end point code in one of middle ware i wanted to map the body content. Commented Dec 13, 2017 at 23:20

1 Answer 1

0

The best way to send binary content is by using an ArrayBufferView or Blobs. The FormData API can be used to send multiple binary items such as application/octet-stream but is inefficient because the base64 encoding adds 33% extra overhead.

For more information, see MDN Web API Reference - XHR Send Method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.