mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 06:08:14 +08:00
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:
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@ namespace Ocelot.RequestBuilder.Builder
|
||||
Stream content,
|
||||
IHeaderDictionary headers,
|
||||
IRequestCookieCollection cookies,
|
||||
string queryString,
|
||||
QueryString queryString,
|
||||
string contentType);
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
{
|
||||
|
Reference in New Issue
Block a user