多部分推杆的Flurl扩展

时间:2018-09-27 17:01:28

标签: flurl

我有一个Flurl实现,已经可以用于多部分的帖子(JSON加文件附件)。我需要为Put添加一个扩展,该扩展的工作原理相同。  到目前为止,我还没有复制一个。

我的邮政编码...

resp = await url
.PostMultipartAsync(mp => mp
    .AddString("manifest", ManifestJSON)
    .AddFile("attachment", new MemoryStream(sendFile.ToArray()), sendFilename));

我将如何构建可比较的“ .PutMultipartAsync”方法来代替它?

2 个答案:

答案 0 :(得分:2)

知道了。

var content = new Flurl.Http.Content.CapturedMultipartContent();
content.AddString("manifest", ManifestJSON);
content.AddFile("attachment", new MemoryStream(sendFile.ToArray()), sendFilename, "application/zip");

resp = await url
.SendAsync(System.Net.Http.HttpMethod.Put, content);

容易

答案 1 :(得分:0)

您所做的应该一次完成就很好。如果您想对其进行真正的增强,使其具有一流的Flurl功能,则可以遵循extensibility pattern并添加以下扩展方法:

public static class MultipartPutExtensions
{
    public static Task<HttpResponseMessage> PutMultipartAsync(this IFlurlRequest request, Action<CapturedMultipartContent> buildContent, CancellationToken cancellationToken = default(CancellationToken))
    {
        var cmc = new CapturedMultipartContent(request.Settings);
        buildContent(cmc);
        return request.SendAsync(HttpMethod.Put, cmc, cancellationToken);
    }

    public static Task<HttpResponseMessage> PutMultipartAsync(this Url url, Action<CapturedMultipartContent> buildContent, CancellationToken cancellationToken = default(CancellationToken))
    {
        return new FlurlRequest(url).PutMultipartAsync(buildContent, cancellationToken);
    }

    public static Task<HttpResponseMessage> PutMultipartAsync(this string url, Action<CapturedMultipartContent> buildContent, CancellationToken cancellationToken = default(CancellationToken))
    {
        return new FlurlRequest(url).PutMultipartAsync(buildContent, cancellationToken);
    }
}

这些是直接在PostMultipartAsync implementations之后建模的。