Skip to content

Commit

Permalink
Add support for json content items (#426)
Browse files Browse the repository at this point in the history
* Add support for json content items
* remove filename from strings
  • Loading branch information
Oren Novotny committed Jan 31, 2018
1 parent 5895c34 commit a893aa3
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions Refit/RequestBuilderImplementation.cs
Expand Up @@ -172,7 +172,7 @@ void AddMultipartItem(MultipartFormDataContent multiPartContent, string fileName

if (itemValue is string stringValue)
{
multiPartContent.Add(new StringContent(stringValue), parameterName, fileName);
multiPartContent.Add(new StringContent(stringValue), parameterName);
return;
}

Expand All @@ -189,7 +189,21 @@ void AddMultipartItem(MultipartFormDataContent multiPartContent, string fileName
return;
}

throw new ArgumentException($"Unexpected parameter type in a Multipart request. Parameter {fileName} is of type {itemValue.GetType().Name}, whereas allowed types are String, Stream, FileInfo, and Byte array", nameof(itemValue));
// Fallback to Json
Exception e = null;
try
{
var stringContent = new StringContent(JsonConvert.SerializeObject(itemValue, settings.JsonSerializerSettings), Encoding.UTF8, "application/json");
multiPartContent.Add(stringContent, parameterName);
return;
}
catch(Exception ex)
{
// Eat this since we're about to throw as a fallback anyway
e = ex;
}

throw new ArgumentException($"Unexpected parameter type in a Multipart request. Parameter {fileName} is of type {itemValue.GetType().Name}, whereas allowed types are String, Stream, FileInfo, Byte array and anything that's JSON serializable", nameof(itemValue), e);
}

Func<HttpClient, CancellationToken, object[], Task<T>> BuildCancellableTaskFuncForMethod<T>(RestMethodInfo restMethod)
Expand Down Expand Up @@ -513,28 +527,7 @@ void AddMultipartItem(MultipartFormDataContent multiPartContent, string fileName
if (enumerable != null)
{
Type tType = null;
var eType = enumerable.GetType();
if (eType.GetTypeInfo().ContainsGenericParameters)
{
tType = eType.GenericTypeArguments[0];
}
else if (eType.IsArray)
{
tType = eType.GetElementType();
}
// check to see if it's one of the types we support for multipart:
// FileInfo, Stream, string or byte[]
if (tType == typeof(Stream) ||
tType == typeof(string) ||
tType == typeof(byte[]) ||
tType.GetTypeInfo().IsSubclassOf(typeof(MultipartItem))
|| tType == typeof(FileInfo)
)
{
typeIsCollection = true;
}
typeIsCollection = true;
}
if (typeIsCollection)
Expand Down

0 comments on commit a893aa3

Please sign in to comment.