title: Returning byte array from server is different when received by the client
body: <p>I have a REST API trying to call (acquire) a byte array from my web api.
  This is my client code:</p>
  
  <pre><code>Uri uri = new Uri(URL);
  httpClient.DefaultRequestHeaders.Accept.Clear();
  httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/Binary"));
  HttpResponseMessage response = await httpClient.GetAsync(uri + route + "?" + COMPANY_REF + "=" + ApplicationObject.CompanyRef);
  response.EnsureSuccessStatusCode();
  var result = response.Content.ReadAsByteArrayAsync().Result
  </code></pre>
  
  <p>This is my server API:</p>
  
  <pre><code>[HttpGet]
  public byte[] Get(Guid companyRef)
  {
      Mapper.CreateMap&lt;Services.Group,Models.Group&gt;();
      var test = Mapper.Map&lt;List&lt;Models.Group&gt;&gt;(groupRepository.Get(companyRef));
      var t = Compression.Serialize(test);
      return t;
  }
  
  
  public static byte[] Serialize(this Object obj)
  {
      if (obj == null)
      {
          return null;
      }
  
      using (var memoryStream = new MemoryStream())
      {
          var binaryFormatter = new BinaryFormatter();
          binaryFormatter.Serialize(memoryStream, obj);
          var compressed = Compression.Zip(memoryStream.ToArray());
  
          return compressed;
      }
  }
  </code></pre>
  
  <p>The byte array returned by the server is:</p>
  
  <p><a href="https://i.stack.imgur.com/fhj4n.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fhj4n.png" alt="enter image description here"></a></p>
  
  <p>the byte array received by the client is:</p>
  
  <p><a href="https://i.stack.imgur.com/19A7F.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/19A7F.png" alt="enter image description here"></a></p>
  
  <p>As you can see it is the wrong size and values.</p>
  
  <p>I am assuming this is down to the media header which is currently set at:</p>
  
  <pre><code>httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/Binary"));
  </code></pre>
  
  <p>Using Fiddler it seems to think it is expecting Json. So I had changed it to 
      'Application/json'</p>
  
  <p>That did nto work.</p>
  
  <p>I also used base64Binary and that did not work either.</p>
  
  <p>What else can I try?</p>
  
  <p>ADDITIONAL:
  Result when I type api direct into browser:</p>
  
  <pre><code>&lt;base64Binary xmlns="http://schemas.microsoft.com/2003/10/Serialization/"&gt;
  H4sIAAAAAAAEAKWS32vUQBDHN3u5TXJRUSr1B4pBX49g/4CDlhb64K9Dq304jjWXTK/rbXePbCKc4N8qCIIgCILvOnO92KDNkxP2m2SYz8zszjKPMfYLjd5kVzjK7jHMxrasMj1M3kDplDWjnfQxPcNkv9ZVXcLIQF2VFDGuZ1rlT2B1ZBdgRqbW2qd0H1+tXAVn6b7VGvIKk7j0EAyUKk+fKle93ZlM/hRKn9kCNAaUtl4Ok/9tYDrtYQdCKmzA9aVTHyCU788z+YzdvrzuZEq7D8OI4BBXhCugH+ZtPP725SiBkU/SJxEkAck6DeWJBiQxSp/iOvKsK6+/XsKJ/zw7g/gAXF6qJR1gz/Pizake1qqgov5PnF7bSRW4zLiccZlzWXAJXJ5wOefylEvF5TsuF+zCwiDgG2N/mbiKEh/ZIlu55NiWC3GNvOfFPNoo7cT7gR1QF//g11F6e1qLGy1MNNj3LmwLZfDaZM6puYFC3GzRQUN/66K3KeLFEoy41eLChvvaxd2hWY/BFMrMxd0WGjXoly70HqF7M5wmdnu/hQ4a9HMX+oAiDqwBkbS4uOE+dXEPKcLg9RCPaEbFxR2JfwNB64eo1AMAAA==
  &lt;/base64Binary&gt;
  </code></pre>
  
  <p>MORE INFO:</p>
  
  <p>If change this:</p>
  
  <pre><code> var result = response.Content.ReadAsByteArrayAsync().Result;
      byte[] resultFromServer = Encoding.ASCII.GetBytes("H4sIAAAAAAAEAKWS32vUQBDHN3u5TXJRUSr1B4pBX49g/4CDlhb64K9Dq304jjWXTK/rbXePbCKc4N8qCIIgCILvOnO92KDNkxP2m2SYz8zszjKPMfYLjd5kVzjK7jHMxrasMj1M3kDplDWjnfQxPcNkv9ZVXcLIQF2VFDGuZ1rlT2B1ZBdgRqbW2qd0H1+tXAVn6b7VGvIKk7j0EAyUKk+fKle93ZlM/hRKn9kCNAaUtl4Ok/9tYDrtYQdCKmzA9aVTHyCU788z+YzdvrzuZEq7D8OI4BBXhCugH+ZtPP725SiBkU/SJxEkAck6DeWJBiQxSp/iOvKsK6+/XsKJ/zw7g/gAXF6qJR1gz/Pizake1qqgov5PnF7bSRW4zLiccZlzWXAJXJ5wOefylEvF5TsuF+zCwiDgG2N/mbiKEh/ZIlu55NiWC3GNvOfFPNoo7cT7gR1QF//g11F6e1qLGy1MNNj3LmwLZfDaZM6puYFC3GzRQUN/66K3KeLFEoy41eLChvvaxd2hWY/BFMrMxd0WGjXoly70HqF7M5wmdnu/hQ4a9HMX+oAiDqwBkbS4uOE+dXEPKcLg9RCPaEbFxR2JfwNB64eo1AMAAA==");
      byte[] actualBytes = Convert.FromBase64String(Encoding.ASCII.GetString(resultFromServer));
  </code></pre>
  
  <p>To this:</p>
  
  <pre><code>var result = response.Content.ReadAsStringAsync().Result;
  byte[] resultFromServer = Encoding.ASCII.GetBytes(result);
  byte[] actualBytes = Convert.FromBase64String(Encoding.ASCII.GetString(resultFromServer));
  </code></pre>
  
  <p>I get the error:</p>
  
  <p><strong>The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.</strong> </p>
  
  <p>This is the value of result</p>
  
  <p>"H4sIAAAAAAAEAKWS32vUQBDHN3u5TXJRUSr1B4pBX49g/4CDlhb64K9Dq304jjWXTK/rbXePb
  CKc4N8qCIIgCILvOnO92KDNkxP2m2SYz8zszjKPMfYLjd5kVzjK7jHMxrasMj1M3kDplDWjnfQ
  xPcNkv9ZVXcLIQF2VFDGuZ1rlT2B1ZBdgRqbW2qd0H1+tXAVn6b7VGvIKk7j0EAyUKk
  +fKle93ZlM/hRKn9kCNAaUtl4Ok/9tYDrtYQdCKmzA9aVTHyCU788z
  +YzdvrzuZEq7D8OI4BBXhCugH
  +ZtPP725SiBkU/SJxEkAck6DeWJBiQxSp/iOvKsK6+/XsKJ/zw7g/gAXF6qJR1gz/Pizake1qq
  gov5PnF7bSRW4zLiccZlzWXAJXJ5wOefylEvF5TsuF
  +zCwiDgG2N/mbiKEh/ZIlu55NiWC3GNvOfFPNoo7cT7gR1QF//g11F6e1qLGy1MNNj3LmwLZfD
  aZM6puYFC3GzRQUN/66K3KeLFEoy41eLChvvaxd2hWY/BFMrMxd0WGjXoly70HqF7M5wmdnu/h
  Q4a9HMX+oAiDqwBkbS4uOE+dXEPKcLg9RCPaEbFxR2JfwNB64eo1AMAAA=="</p>
  
  <p>From Debug.Write:</p>
  
  <p>"H4sIAAAAAAAEAKWS32vUQBDHN3u5TXJRUSr1B4pBX49g/4CDlhb64K9Dq304jjWXTK/rbXePbCKc4N8qCIIgCILvOnO92KDNkxP2m2SYz8zszjKPMfYLjd5kVzjK7jHMxrasMj1M3kDplDWjnfQxPcNkv9ZVXcLIQF2VFDGuZ1rlT2B1ZBdgRqbW2qd0H1+tXAVn6b7VGvIKk7j0EAyUKk+fKle93ZlM/hRKn9kCNAaUtl4Ok/9tYDrtYQdCKmzA9aVTHyCU788z+YzdvrzuZEq7D8OI4BBXhCugH+ZtPP725SiBkU/SJxEkAck6DeWJBiQxSp/iOvKsK6+/XsKJ/zw7g/gAXF6qJR1gz/Pizake1qqgov5PnF7bSRW4zLiccZlzWXAJXJ5wOefylEvF5TsuF+zCwiDgG2N/mbiKEh/ZIlu55NiWC3GNvOfFPNoo7cT7gR1QF//g11F6e1qLGy1MNNj3LmwLZfDaZM6puYFC3GzRQUN/66K3KeLFEoy41eLChvvaxd2hWY/BFMrMxd0WGjXoly70HqF7M5wmdnu/hQ4a9HMX+oAiDqwBkbS4uOE+dXEPKcLg9RCPaEbFxR2JfwNB64eo1AMAAA=="</p>
  
  <p>To make this finally work I had to do this:</p>
  
  <pre><code>byte[] actualBytes = Convert.FromBase64String(result2.Replace("\"",""));
  </code></pre>
  
  <p>Thanks</p>