title: Losing bytes from a byte[] when sent in a HttpResponseMessage and interpreted in an angular promise
body: <p>I've come across a problem in a transfer of information over the wire that is confusing me. </p>
  
  <p>I have a WebAPI endpoint. When called it asks for a particular file and then serves it as a bytearraycontent</p>
  
  <pre><code>public class SampleType
  {
      public string Url { get; set; }
      public string Id { get; set; }
  }
  
  [HttpPost]
  [Route("document")]
  public async Task&lt;HttpResponseMessage&gt; BuildDocumentAsync(HttpRequestMessage request, SampleType candidateInput)
  {
      var document = await _applicationService.GetByIdAsync(candidateInput.Id);
      var generatedDocument = new CandidatePdfDocumentBuilder().BuildDocument(document, candidateInput.Url);
  
      var response = request.CreateResponse(HttpStatusCode.OK);
      response.Content = new ByteArrayContent(generatedDocument);
      response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
  
      return response;
  }
  </code></pre>
  
  <p>I can see that the document build here has a size of, in my test case, 53993 bytes. It is a PDF document. I have written the document to disk in my test case, and the size of the file on disk agrees with the number bytes in my <code>generatedDocument</code>.</p>
  
  <p>I have an angular architecture that communicates with this.</p>
  
  <p>An infrastructure service that wraps <code>$http</code></p>
  
  <pre><code>(function () {
      "use strict";
      var httpModelService = function ($http) {
  
          this.postTo = function (endpoint, model) {
              return $http.post(endpoint, model);
          }
      };
  
      var app = angular.module("myApplicationName");
      app.service("httpModelService", httpModelService);
  }());
  </code></pre>
  
  <p>A service</p>
  
  <pre><code>this.buildDocument = function (documentID) {
      return httpModelService.postTo('release-protocol/document', { url: $location.absUrl(), id: documentID });
  };
  </code></pre>
  
  <p>A controller</p>
  
  <pre><code>releaseProtocolService.buildDocument($scope.document.id).
      then(function(response) {
          var dataSize = response.data.length;
              saveData(response.data, $scope.document.documentName + '.pdf');
          });
  </code></pre>
  
  <p>I can see, however, that if I examine the length of response.data, in my case this is a smaller number of bytes - 52137 in my case.</p>
  
  <p>With all of this background out of the way, what could be causing me to have fewer bytes available in my <code>response.data</code>?</p>