Fluent.Http
Fluent.Http copied to clipboard
[Feature Request] Multipart form support
Hey there, awesome library!
Wanted to float the idea of mulitpart form support.
Example
I recently had an example where i had a form-data endpoint that I would hit just fine with a RestSharp call like below, but HttpClient syntax was much more involved:
var client = new RestClient("https://localhost:5005/api/eventattachments?eventid=some-guid-here");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddFile("File", "/my/file/path.pdf");
request.AddParameter("Filename", "path.pdf");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
For context, my controller looks like this:
[Consumes("multipart/form-data")]
[Produces("application/json")]
[DisableRequestSizeLimit]
[HttpPost(Name = "AddEventAttachment")]
public async Task<ActionResult<EventAttachmentDto>> AddEventAttachment([FromForm]EventAttachmentForCreationDto eventAttachmentForCreation,
[FromQuery(Name = "eventId"), BindRequired] Guid eventId)
{
var command = new AddEventAttachment.AddEventAttachmentCommand(eventAttachmentForCreation, eventId);
var commandResponse = await _mediator.Send(command);
var response = new Response<EventAttachmentDto>(commandResponse);
return CreatedAtRoute("GetEventAttachment",
new { Id = commandResponse.Id },
response);
}
and the DTO coming in looks like so:
public class EventAttachmentForCreationDto
{
public IFormFile File { get; set; }
public string FileName { get; set; }
public string Comments { get; set; }
public int? SortOrder { get; set; }
}
Request
It would be fantastic if you were able to provide an api that is more simplified like the restclient call, but could leverage HttpClient directly.
Maybe something like this?
// _client is an `HttpClient` instance from integ tests
var request = await FluentHttpClient.Build(_client)
.Request(() => Post(dog).WithUrl("api/eventattachments?eventid=some-guid-here"));
// for tests, it would probably be easier to read a stream? not going to have a file in my project just for a test.
request.AddFile("File", "/my/file/path.pdf");
request.AddParameter("Filename", "path.pdf");
var response = await request.ExecuteAsync();
response.Content.Should()....
Hey @pdevito3 I really like this idea can the file be added to the HttpRequestMessage if so we can add this to the following builder with some nice fluent methods much like what is provided by RestClient see here https://github.com/mumby0168/Fluent.Http/blob/main/src/Fluent.Http/HttpRequestMessageBuilder.cs
If it cannot be added to the HttpRequestMessage we may have to think of another way. Happy to look at adding this either way.
Yeah looks like that's what restsharp is doing, so should be fine if there was a WithParameter and/or, more importantly, a WithFile that could take a stream.
Yeah looks like that's what restsharp is doing, so should be fine if there was a
WithParameterand/or, more importantly, aWithFilethat could take a stream.
Yes I think this sounds like a good idea, assuming you would want this to support uploading multiple files also? As in being able to call something like WithFile(...).WithFile(...) ?
yeah, personally i only need to do one at a time in this case, but multi file would probably be a good idea for broader flexibility.
yeah, personally i only need to do one at a time in this case, but multi file would probably be a good idea for broader flexibility.
Was this something you fancied contributing?
not sure it's something i have the bandwidth for atm. maybe at some point, but i'm honestly not even sure exactly what the httpclient syntax looks like to layer the API on to yet.
regardless, wanted to float the idea as something to keep in mind and if you can fit it in, that's great! I maintain my own OSS so i know what it's like getting hit with feature requests coming in. hopefully i'll have some time to throw at this eventually though. would be fun to add to.
not sure it's something i have the bandwidth for atm. maybe at some point, but i'm honestly not even sure exactly what the httpclient syntax looks like to layer the API on to yet.
regardless, wanted to float the idea as something to keep in mind and if you can fit it in, that's great! I maintain my own OSS so i know what it's like getting hit with feature requests coming in. hopefully i'll have some time to throw at this eventually though. would be fun to add to.
Yeah sounds good, understand how it is I will try and get to it soon, if not tho feel free to raise a PR 😃