title: Passing an image from a Wep Api service to another Web Api service
body: <p>For security reasons, I am building two Web Api services. The first Web Api app will have access to an image generating service, and will act as a security proxy. The second Web Api app will call the first app from the internet and retrieve the image.</p>
  
  <p>However, I can't seem to get to negotiate passage of the image correctly. My thought was to have the security proxy Web API to get the image, and then pass it as a byte array my other service which would allow a user to download the image. However, when my browser attempts to open the image, it is always corrupted.</p>
  
  <p>Here is the security proxy getting the image, which I know is successful:</p>
  
  <pre><code>public byte[] Get(string invoice, string Customer)
      {
          object image;
          try
          {
              image = _repo.GetImage(invoice, Customer);
          }
          catch (ApplicationException exc)
          {
              var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
              {
                  Content = new StringContent(string.Format("No Image with Invoice Number = {0}", invoice.ToString())),
                  ReasonPhrase = "Image Not Found"
              };
              throw new HttpResponseException(resp);
          }
          catch (Exception exc)
          {
              throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
  
          }
          return (byte[])image;
      }
  </code></pre>
  
  <p>This returns an array with a length of 40133.</p>
  
  <p>The calling Web API service looks like this:</p>
  
  <pre><code>public HttpResponseMessage Get(string invoice, string Customer)
      {           
  
          HttpClient client = new HttpClient();
  
          client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
  
          byte[] img = client.GetByteArrayAsync("http://localhost:1363/api/Image/" + invoice + "/" + Customer).Result;
  
          HttpResponseMessage response = new HttpResponseMessage();
          response.Content = new ByteArrayContent(img);
          response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/tiff");
          var disposition = new ContentDispositionHeaderValue("attachment");
          disposition.FileName = "ImageDocument.tif";
          response.Content.Headers.ContentDisposition = disposition;
  
          return response;
  
      }
  </code></pre>
  
  <p>However, the length of the img byte array is 53514.</p>
  
  <p>When the browser tries to open the image, it tells me it is corrupt. If I open the TIFF in notepad, I get :</p>
  
  <pre><code>"SUkqAAgAAAASAP4ABAABAAAAAAAAAAABBAABAAAAsAYAAAEBBA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waTMGsjgqGA1yOGaUA0Hg0igC5qZ6I1GSsNMuGqeBrI+bBoNYQrfNIiMREWdl4zVWRERkQzVECBpNcRyMCz6PhQgZwQGQLjpCIWwgxERGLLYAx//zLWLx4IeDnBnxSGFMRgIeZ4zcaR+KuPM4KeZ6MBTqKcj8YjAQ4IejDoQ4eE07WGnra3p9w07Xhw1s7NHu+0/v+/SQf6/9+cjwp0Z0Z8KeCm4p4IGQwhoz4cwCFBZN8u8s5duXeXTLva7pN6J56l45sf8u8u
  </code></pre>
  
  <p><em><strong>SNIP</em>*</strong></p>
  
  <p>Anyone know what I am doing wrong?</p>
  
  <p>Thanks!</p>
  
  <p>Chris</p>
  
  <p><strong>Solved</strong></p>
  
  <p>If anyone is interested in the calling code that leverages the solution identified, here it is:</p>
  
  <pre><code>public HttpResponseMessage Get(string invoice, string Customer)
      {
  
          HttpClient client = new HttpClient();
  
          client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/tiff"));
  
          byte[] img = client.GetByteArrayAsync("http://localhost:1363/api/Image/" + invoice + "/" + Customer).Result;
  
          HttpResponseMessage response = new HttpResponseMessage();
          response.Content = new ByteArrayContent(img);
          response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/tiff");
          var disposition = new ContentDispositionHeaderValue("attachment");
          disposition.FileName = "ImageDocument.tif";
          response.Content.Headers.ContentDisposition = disposition;
  
          return response;
  
  
      }
  </code></pre>