Search code examples
.netasp.net-coregzipphp-ziparchive

415 UnsupportedMediaType when trying to intercept a POST request


My feed provider send me a .gz file (zip file) by POST request to my server.

I'm trying to implement the .NET code which will intercept POST request and unzip the file to open the file inside.

I'm just trying to intercept the POST request and unzip the content doing this :

namespace app.Controllers
{
    [Route("")]
    public class FeedController : Controller
    {
        [HttpPost]
        public string Post([FromBody] string content)
        {
            return content;
        }
    }
}

it returns 415 UnsupportedMediaType.

How to intercept POST request which is a ZIP File, and how to unzip it to return the file inside ?

Edit :

[HttpPost]
        [Consumes("multipart/form-data")]
        public IActionResult Post(IFormFile file)
        {
            if (file == null)
                return BadRequest();

            try
            {
                using (var zip = new ZipArchive(file.OpenReadStream()))
                {
                    // do stuff with the zip file
                }
            }
            catch
            {
                return BadRequest();
            }

            return Ok();
        }

Solution

  • A part of the problem is solved.

    To read a .gz file sended by POST request you should do this :

    [HttpPost]
            [Consumes("application/gzip")]
            public IActionResult Post(IFormFile file)
            {
    
                    WebClient Client = new WebClient();
                    Client.DownloadFile("http://xxxxxx.com/feed.gz", "C:\\temp\\mygzipfile.gz");
                    // do something with this file
                return Ok();
            }