Skip to content
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

Support for multipart/form-data requests (including files) #113

Closed
tmenier opened this issue Jul 14, 2016 · 5 comments
Closed

Support for multipart/form-data requests (including files) #113

tmenier opened this issue Jul 14, 2016 · 5 comments

Comments

@tmenier
Copy link
Owner

tmenier commented Jul 14, 2016

mulipart/form-data requests will be supported in Flurl.Http 1.0 via the following syntax:

var resp = await "http://api.com"
    .PostMultipartAsync(mp => mp
        .AddString("name", "hello!")                // individual string
        .AddStringParts(new {a = 1, b = 2})         // multiple strings
        .AddFile("file1", path1)                    // local file path
        .AddFile("file2", stream, "foo.txt")        // file stream
        .AddJson("json", new { foo = "x" })         // json
        .AddUrlEncoded("urlEnc", new { bar = "y" }) // URL-encoded                      
        .Add(content));                             // any HttpContent

In the above example, mp is a CapturedMultiplartContent, which can be built fluently inline as shown, or separately. It contains a read-only Parts array of HttpContent objects that have been added, which can later be inspected.

@tmenier
Copy link
Owner Author

tmenier commented Jul 15, 2016

This feature is now available in the latest prerelease:

https://www.nuget.org/packages/Flurl.Http/1.0.0-beta8

@ecentinela
Copy link

Hi @tmenier , fantastic! Can you post an example about preparing the CapturedMultipartContent separately? Thanks!

@tmenier
Copy link
Owner Author

tmenier commented Jul 15, 2016

@ecentinela

var content = new CapturedMultipartContent()
   .Add......

await new FlurlClient(url).SendAsync(HttpMethod.Post, content);

// or

await "http://api.com".WithClient(fc).SendAsync(HttpMethod.Post, content);

@tmenier
Copy link
Owner Author

tmenier commented Jul 17, 2016

@ecentinela The sample above made me realize there ought to be extension methods on string and Url to match FlurlClient's SendAsync. Further, the overloads of PostAsync, PutAsync, and PatchAsync that took no content argument now take a (nullable) HttpContent object, since those verbs normally have some sort of body.

So, in the previous example, you can now build CapturedMultipartContent and simply post it like this:

await "http://api.com".PostAsync(content);

I did a beta9 for this if you want to test it, but I won't delay the full 1.0 release much longer.

https://www.nuget.org/packages/Flurl.Http/1.0.0-beta9

@eestein
Copy link

eestein commented Dec 8, 2016

@tmenier I'm trying to upload a file with some body. Is PostMultipartAsync the only way?

On my C# code I have this:

var resource = FormBind<StorageFileResource>();
var file = Request.Files.First().ToPostedFile();

FormBind reads data from the request and fills the object.

By using PostMultipartAsync I know it should start like this:

.PostMultipartAsync((mp) => { mp.AddFile(name, stream, name)}), but I can't figure out how to add the object. Do you have any ideas on that?

This is my current try:

public static async Task<T> PostFileAsync<T>(string url, object data, string name, Stream stream, object queryString = null)
	where T : class
{
	return await HandleRequest(async () => queryString != null
		? await url
			.SetQueryParams(queryString)
			.SetClaimsToken()
	                           .PostMultipartAsync((mp) => { mp.AddFile(name, stream, name)})
		  	.ReceiveJson<T>()
		: await url
			.SetClaimsToken()
			.PostMultipartAsync((mp) => mp.AddFile(name, stream, name))
		 	.ReceiveJson<T>());
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants