Passing an image from a Wep Api service to another Web Api service
asp.net, asp.net-web-api
Solution
With your above current return type (byte[]) of action, formatters of web api are probably handling them and hence you are seeing unexpected response.
can you try sending the image as a ByteArrayContent instead?(you need to have HttpResponseMessage as a return type here)
Example:
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(..your byte array here...);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpeg");
return response;
Problem
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. 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. Here is the security proxy getting the image, which I know is successful: ``` 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; } ``` This returns an array with a length of 40133. The calling Web API service looks like this: ``` 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; } ``` However, the length of the img byte array is 53514. When the browser tries to open the image, it tells me it is corrupt. If I open the TIFF in notepad, I get : ``` "SUk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AgjhC5lQEI+VYoiIiIiIiIiJXLAazgaZvMBqEcNI0BoJwaTMGsjgqGA1yOGaUA0Hg0igC5qZ6I1GSsNMuGqeBrI+bBoNYQrfNIiMREWdl4zVWRERkQzVECBpNcRyMCz6PhQgZwQGQLjpCIWwgxERGLLYAx//zLWLx4IeDnBnxSGFMRgIeZ4zcaR+KuPM4KeZ6MBTqKcj8YjAQ4IejDoQ4eE07WGnra3p9w07Xhw1s7NHu+0/v+/SQf6/9+cjwp0Z0Z8KeCm4p4IGQwhoz4cwCFBZN8u8s5duXeXTLva7pN6J56l45sf8u8u ``` SNIP* Anyone know what I am doing wrong? Thanks! Chris Solved If anyone is interested in the calling code that leverages the solution identified, here it is: ``` 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; } ```