0

I'm developing an API who makes searches on a stock photos and now I need to deliver files(Download method) so I created the following code:

public HttpResponseMessage Download(string id)
{
    Download download = new Download(id, apiUser);
    byte[] myDataBuffer = download.DownloadImage();

    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
    result.Content = new ByteArrayContent(myDataBuffer);
    result.Content.Headers.ContentType =
    new MediaTypeHeaderValue("application/octet-stream");

    return result;
}

The bytes are received correctly on method DownloadImage(), the problem is in the HttpResponseMessage return, I upload in release website and through another project I have the following code who calls the API:

public static void DownloadNewImage(string id)
{

    var request = new RestRequest(Method.GET);
    request.Resource = "Home/Download";
    request.AddParameter("id", id);
    RestResponse response = Execute(request, "website-url");
}

private static RestResponse Execute(RestRequest request, string url)
{
    var cliente = new RestClient(url);
    cliente.FollowRedirects = false;
    var response = cliente.Execute(request);          
    return (RestResponse)response;
}

When i debug the response, I receive the following:

enter image description here

See that ContentLength = -1 and when i open the Content text the retur is this:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content:                    
System.Net.Http.ByteArrayContent, Headers:
{
    Content-Type: application/octet-stream
}

I think that is something wrong in the Download method from API who returns HttpResponseMessage, and some parameters is wrong someone can give me help?

Leonardo Bonetti
  • 197
  • 1
  • 10

0 Answers0