diff --git a/src/Ocelot/Multiplexer/MultiplexingMiddleware.cs b/src/Ocelot/Multiplexer/MultiplexingMiddleware.cs index 9c8d1a612..70d8d5df6 100644 --- a/src/Ocelot/Multiplexer/MultiplexingMiddleware.cs +++ b/src/Ocelot/Multiplexer/MultiplexingMiddleware.cs @@ -172,6 +172,7 @@ private static HttpContext Copy(HttpContext source) target.Request.Body = source.Request.Body; target.Request.ContentLength = source.Request.ContentLength; target.Request.ContentType = source.Request.ContentType; + target.Request.Form = source.Request.Form; target.Request.Host = source.Request.Host; target.Request.Method = source.Request.Method; target.Request.Path = source.Request.Path; diff --git a/src/Ocelot/Request/Mapper/RequestMapper.cs b/src/Ocelot/Request/Mapper/RequestMapper.cs index 8a5bba465..f8a6acb2a 100644 --- a/src/Ocelot/Request/Mapper/RequestMapper.cs +++ b/src/Ocelot/Request/Mapper/RequestMapper.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Net.Http; using System.Threading.Tasks; - + using Ocelot.Configuration; using Microsoft.AspNetCore.Http; @@ -41,6 +41,10 @@ public async Task> Map(HttpRequest request, Downstr } } + private static bool IsMultipartContentType(string contentType) + => !string.IsNullOrEmpty(contentType) + && contentType.Contains("multipart/form-data", StringComparison.OrdinalIgnoreCase); + private static async Task MapContent(HttpRequest request) { if (request.Body == null || (request.Body.CanSeek && request.Body.Length <= 0)) @@ -49,12 +53,39 @@ private static async Task MapContent(HttpRequest request) } // Never change this to StreamContent again, I forgot it doesnt work in #464. - var content = new ByteArrayContent(await ToByteArray(request.Body)); + HttpContent content; + + if (IsMultipartContentType(request.ContentType)) + { + content = new MultipartFormDataContent(); + if (request.Form != null && request.Form.Files != null) + { + foreach (var f in request.Form.Files) + { + using var memStream = new MemoryStream(); + await f.CopyToAsync(memStream); + var fileContent = new ByteArrayContent(memStream.ToArray()); + ((MultipartFormDataContent)content).Add(fileContent, f.Name, f.FileName); + } + } - if (!string.IsNullOrEmpty(request.ContentType)) + if (request.Form != null) + { + foreach (var key in request.Form.Keys) + { + var strContent = new StringContent(request.Form[key]); + ((MultipartFormDataContent)content).Add(strContent, key); + } + } + } + else { - content.Headers - .TryAddWithoutValidation("Content-Type", new[] { request.ContentType }); + content = new ByteArrayContent(await ToByteArray(request.Body)); + if (!string.IsNullOrEmpty(request.ContentType)) + { + content.Headers + .TryAddWithoutValidation("Content-Type", new[] { request.ContentType }); + } } AddHeaderIfExistsOnRequest("Content-Language", content, request); @@ -108,11 +139,9 @@ private static async Task ToByteArray(Stream stream) { await using (stream) { - using (var memStream = new MemoryStream()) - { - await stream.CopyToAsync(memStream); - return memStream.ToArray(); - } + using var memStream = new MemoryStream(); + await stream.CopyToAsync(memStream); + return memStream.ToArray(); } } }