fixed a bug where getting invalid parameter error if sending a steam content with no stream, now we try convert to byte array which the c# http client is happy to take if empty...or this error is caused because we are trying to use a stream when we shouldnt.

This commit is contained in:
TomPallister
2016-10-28 22:50:00 +01:00
parent 6be3c1cf73
commit 3a1dd1f9bc
7 changed files with 49 additions and 18 deletions

View File

@ -12,17 +12,18 @@ namespace Ocelot.RequestBuilder.Builder
public class HttpRequestBuilder : IRequestBuilder
{
public async Task<Response<Request>> Build(string httpMethod, string downstreamUrl, Stream content, IHeaderDictionary headers,
IRequestCookieCollection cookies, string queryString, string contentType)
IRequestCookieCollection cookies, QueryString queryString, string contentType)
{
var method = new HttpMethod(httpMethod);
var uri = new Uri(string.Format("{0}{1}", downstreamUrl, queryString));
var uri = new Uri(string.Format("{0}{1}", downstreamUrl, queryString.ToUriComponent()));
var httpRequestMessage = new HttpRequestMessage(method, uri);
if (content != null)
{
httpRequestMessage.Content = new StreamContent(content);
httpRequestMessage.Content = new ByteArrayContent(ToByteArray(content));
}
if (!string.IsNullOrEmpty(contentType))
@ -35,11 +36,7 @@ namespace Ocelot.RequestBuilder.Builder
if (headers != null)
{
headers.Remove("Content-Type");
}
//todo get rid of if
if (headers != null)
{
foreach (var header in headers)
{
//todo get rid of if..
@ -63,5 +60,17 @@ namespace Ocelot.RequestBuilder.Builder
return new OkResponse<Request>(new Request(httpRequestMessage, cookieContainer));
}
private byte[] ToByteArray(Stream stream)
{
using (stream)
{
using (MemoryStream memStream = new MemoryStream())
{
stream.CopyTo(memStream);
return memStream.ToArray();
}
}
}
}
}

View File

@ -12,7 +12,7 @@ namespace Ocelot.RequestBuilder.Builder
Stream content,
IHeaderDictionary headers,
IRequestCookieCollection cookies,
string queryString,
QueryString queryString,
string contentType);
}
}

View File

@ -34,7 +34,7 @@ namespace Ocelot.RequestBuilder.Middleware
var request = await _requestBuilder
.Build(context.Request.Method, downstreamUrl.Data, context.Request.Body,
context.Request.Headers, context.Request.Cookies, context.Request.QueryString.Value, context.Request.ContentType);
context.Request.Headers, context.Request.Cookies, context.Request.QueryString, context.Request.ContentType);
if (request.IsError)
{