mercredi 21 août 2019

Get a file from public web API which consumes DMZ API

I have two APIs:

  1. Public API
  2. API in DMZ

I need to implement a method which returns a file in a public API.

API in DMZ:

    public HttpResponseMessage GetContent(int id)
    {
        var content = from m in db.messagestoimages
                      where m.Message == id
                      select m;

        if (content == null || content.Count() == 0)
        {
            return null;
        }
        string fileName = content.First().ImageURL;

        string fullPath = AppDomain.CurrentDomain.BaseDirectory  + fileName;
        if (File.Exists(fullPath))
        {

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            var fileStream = new FileStream(fullPath, FileMode.Open);
            response.Content = new StreamContent(fileStream);
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = fileName;
            return response;
        }

        return null;
    }

Public API

    public HttpResponseMessage Get(string id)
    {
        try
        {
            //if (form.file != null && form.file.Length > 0)
            {
                using (var client = new HttpClient())
                {
                    try
                    {
                        string host = configuration.GetSection("MySettings").GetSection("OctopusURL").Value;

                        client.BaseAddress = new Uri(host);

                        var response =  client.GetAsync("api/Content/" + id);


                        return response.Result;

                    }
                    catch (Exception ex)
                    {
                        return new HttpResponseMessage(HttpStatusCode.NotFound);
                    }
                }
            }

        }
        catch (Exception ex)
        {
            return new HttpResponseMessage(HttpStatusCode.NotFound); // 500 is generic server error
        }
    }

if i call directly DMZ method, file is downloaded succesfully. If i call public API, i do not get a file. Only content { "version": { "major": 1, "minor": 1, "build": -1, "revision": -1, "majorRevision": -1, "minorRevision": -1 }, "content": { "headers": [ { "key": "Content-Length", "value": [ "17488" ] }, { "key": "Content-Type", "value": [ "application/octet-stream" ] }, { "key": "Expires", "value": [ "-1" ] }, { "key": "Content-Disposition", "value": [ "attachment; filename=\"/Userimage/3A297B090A41B649BF80.jpeg\"" ] } ] }, "statusCode": 200, "reasonPhrase": "OK", "headers": [ { "key": "Cache-Control", "value": [ "no-cache" ] }, { "key": "Pragma", "value": [ "no-cache" ] }, { "key": "Server", "value": [ "Microsoft-IIS/10.0" ] }, { "key": "X-AspNet-Version", "value": [ "4.0.30319" ] }, { "key": "X-SourceFiles", "value": [ "=?UTF-8?B?QzpcVXNlcnNcbmFyaW1cc291cmNlXHJlcG9zXENoYXRBc3Npc3RlbnRcQ2hhdEFzc2lzdGFudFxDaGF0QXNzaXN0ZW50XGFwaVxDb250ZW50XDkzMjQ=?=" ] }, { "key": "X-Powered-By", "value": [ "ASP.NET" ] }, { "key": "Date", "value": [ "Wed, 21 Aug 2019 13:43:26 GMT" ] } ], "requestMessage": { "version": { "major": 2, "minor": 0, "build": -1, "revision": -1, "majorRevision": -1, "minorRevision": -1 }, "content": null, "method": { "method": "GET" }, "requestUri": "http://localhost:60236/api/Content/9324", "headers": [], "properties": {} }, "isSuccessStatusCode": true }

How can i solve this problem?




Aucun commentaire:

Enregistrer un commentaire