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
Adapt .MultiPart to support enhanced file upload #110
Comments
Hey @RipaBogdan, sure, I'm happy to see if I can support that. To make it a little easier for me to implement, do you maybe have an example of what the final request should look like? Ideally sourced from a tool like Fiddler or WireShark. Once I know that I'll think of a syntax that would support it, show that to you and if you're happy with it I'm happy to build in support for it. |
Hi @basdijkstra, unfortunately I don't use these tools, however I can provide this as an example of what I'm talking about: |
Thanks for that. I'll see if I can find some more information on this. By the way, it looks like this request is generated when some kind of web form is submitted. You wouldn't by any chance have a public example of such a form available for me to use / test, would you? |
@RipaBogdan do you have an example of such a form for me? Without a way to see what the request should look like it's going to be a little hard to implement this as I can't write tests for it. |
unfortunately the form data above is all I have as an example, but the idea remains the same, we need a way to upload a file along with other fields. |
Got it. I’ll do some more research and will try to provide you with a test version soon. |
I think I'm getting somewhere here, but it does require me to rewrite some of the internals of how a request body is created. It would be an improvement, though, so I'm happy to do it, but it might take me a bit more time. Will keep you posted, @RipaBogdan. |
@RipaBogdan can you tell me if there is a wrapper around the various elements in the screenshot you attached in #110 (comment) ? This is what I've got now: I could probably very easily allow for more inner sections to be added here, and I think this is the way to do it as a System.Net.Http.ByteArrayContent And the So, again, is there are wrapper around the payload sections you showed in that screenshot? Can I see the entire payload? |
Note to self: I think this is the way to do it: https://brokul.dev/sending-files-and-additional-data-using-httpclient-in-net-core. Let me try and implement this over the weekend. |
Well, that was easier than expected. [Test]
public void AdditionalFieldsCanBeUploadedWithAFileUsingADictionary()
{
Dictionary<string, HttpContent> additionalMultipartPayload = new Dictionary<string, HttpContent>()
{
{ "projectId", new StringContent("PROJECT-1234") },
{ "projectName", new StringContent("MyProject") },
};
Given()
.MultiPart(new FileInfo(this.csvFileName))
.MultiPart(additionalMultipartPayload)
.When()
.Post($"{MOCK_SERVER_BASE_URL}/csv-multipart-form-data-additional-fields")
.Then()
.StatusCode(201);
}
You can even add the file itself through the Dictionary, too, but you'll need to convert it to an object of type Dictionary<string, HttpContent> multipartPayloadIncludingFile = new Dictionary<string, HttpContent>()
{
{ "file", new StreamContent(fileInfo.OpenRead()) },
{ "projectId", new StringContent("PROJECT-1234") },
{ "projectName", new StringContent("MyProject") },
}; where fileInfo is the @RipaBogdan what do you think? I've just published a new beta version so you can try it out for yourself, it's Please let me know what you think once you've tested it so I can release a proper 4.2.0 version soon (I've been planning to do this for a while). |
@basdijkstra I have tested .MultiPart and can confirm that this solution is working as expected. |
Awesome, thank you @RipaBogdan for your support. I'll release version 4.2.0 later this week, I want to do some final testing tomorrow (around .NET 8 support, not this feature). Please note that I did change the order of arguments for one of the |
Just a quick heads up in case you missed it, @RipaBogdan: I released version 4.2.0 last week, including this feature. Thanks again and let me know if there’s anything else you think is missing or misbehaving! |
Hi @basdijkstra, there is a small thing that is giving us trouble regarding this .MultiPart file upload, that maybe can be something quick from your side. I suspect this is causing our issues, since our endpoint looks for name="content". Thank you, |
Hey @RipaBogdan, the old way of specifying a multipart file upload with the option to specify a custom control name is still available, would that work for you in this case? See
|
Unfortunately, it seems that we need a mix between the two,
|
Have you tried doing just that? Because the way it’s implemented each additional call to
I even have a test for it myself:
If it doesn’t work as you expected my tests aren’t doing what they should :) |
Indeed, it works, just needed to switch the parameters around:
Thank you! |
No problem, you’re welcome! |
Can .MultiPart be addapted in order to allow dictionaries as input?
There are situations where you need to upload a file and some additional details are required.
Current implementation:
MultiPart(FileInfo fileName, string controlName="file")
Would maybe require to accept a dictionary of type <string, object>, eg:
The text was updated successfully, but these errors were encountered: