8000 Feature- multipart data name to be specified at the moment of execution. by farcasclaudiu · Pull Request #1002 · reactiveui/refit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Feature- multipart data name to be specified at the moment of execution. #1002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,12 @@ At this time, multipart methods support the following parameter types:
- Stream
- FileInfo

The parameter name will be used as the name of the field in the multipart data. This can be overridden with the `AliasAs` attribute.
Name of the field in the multipart data priority prec 8000 edence:

* multipartItem.Name if specified and not null (optional); dynamic, allows naming form data part at execution time.
* [AliasAs] attribute (optional) that decorate the streamPart parameter in the method signature (see below); static, defined in code.
* MultipartItem parameter name (default) as defined in the method signature; static, defined in code.

A custom boundary can be specified with an optional string parameter to the `Multipart` attribute. If left empty, this defaults to `----MyGreatBoundary`.

To specify the file name and content type for byte array (`byte[]`), `Stream` and `FileInfo` parameters, use of a wrapper class is required.
Expand Down
31 changes: 31 additions & 0 deletions Refit.Tests/MultipartTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,37 @@ public async Task MultipartUploadShouldWorkWithStreamPart()
var result = await fixture.UploadStreamPart(new StreamPart(stream, "test-streampart.pdf", "application/pdf"));
}

[Fact]
public async Task MultipartUploadShouldWorkWithStreamPartWithNamedMultipart()
{
var handler = new MockHttpMessageHandler
{
Asserts = async content =>
{
var parts = content.ToList();

Assert.Single(parts);

Assert.Equal("test-stream", parts[0].Headers.ContentDisposition.Name);
Assert.Equal("test-streampart.pdf", parts[0].Headers.ContentDisposition.FileName);
Assert.Equal("application/pdf", parts[0].Headers.ContentType.MediaType);

using var str = await parts[0].ReadAsStreamAsync();
using var src = GetTestFileStream("Test Files/Test.pdf");
Assert.True(StreamsEqual(src, str));
}
};

var settings = new RefitSettings()
{
HttpMessageHandlerFactory = () => handler
};

using var stream = GetTestFileStream("Test Files/Test.pdf");
var fixture = RestService.For<IRunscopeApi>(BaseAddress, settings);
var result = await fixture.UploadStreamPart(new StreamPart(stream, "test-streampart.pdf", "application/pdf", "test-stream"));
}

[Fact]
public async Task MultipartUploadShouldWorkWithStreamPartAndQuery()
{
Expand Down
19 changes: 13 additions & 6 deletions Refit/MultipartItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public MultipartItem(string fileName, string contentType)
ContentType = contentType;
}

public MultipartItem(string fileName, string contentType, string name) : this(fileName, contentType)
{
Name = name;
}

public string Name { get; }

public string ContentType { get; }

public string FileName { get; }
Expand All @@ -33,8 +40,8 @@ public HttpContent ToContent()

public class StreamPart : MultipartItem
{
public StreamPart(Stream value, string fileName, string contentType = null) :
base(fileName, contentType)
public StreamPart(Stream value, string fileName, string contentType = null, string name = null) :
base(fileName, contentType, name)
{
Value = value ?? throw new ArgumentNullException("value");
}
Expand All @@ -49,8 +56,8 @@ protected override HttpContent CreateContent()

public class ByteArrayPart : MultipartItem
{
public ByteArrayPart(byte[] value, string fileName, string contentType = null) :
base(fileName, contentType)
public ByteArrayPart(byte[] value, string fileName, string contentType = null, string name = null) :
base(fileName, contentType, name)
{
Value = value ?? throw new ArgumentNullException("value");
}
Expand All @@ -65,8 +72,8 @@ protected override HttpContent CreateContent()

public class FileInfoPart : MultipartItem
{
public FileInfoPart(FileInfo value, string fileName, string contentType = null) :
base(fileName, contentType)
public FileInfoPart(FileInfo value, string fileName, string contentType = null, string name = null) :
base(fileName, contentType, name)
{
Value = value ?? throw new ArgumentNullException("value");
}
Expand Down
2 changes: 1 addition & 1 deletion Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ async Task AddMultipartItemAsync(MultipartFormDataContent multiPartContent, stri
if (itemValue is MultipartItem multipartItem)
{
var httpContent = multipartItem.ToContent();
multiPartContent.Add(httpContent, parameterName, string.IsNullOrEmpty(multipartItem.FileName) ? fileName : multipartItem.FileName);
multiPartContent.Add(httpContent, multipartItem.Name ?? parameterName, string.IsNullOrEmpty(multipartItem.FileName) ? fileName : multipartItem.FileName);
return;
}

Expand Down
0