gpt4 book ai didi

How to use MultipartFormDataContent with Azure.Core RequestContent(如何将MultipartFormDataContent与Azure一起使用。Core RequestContent)

转载 作者:bug小助手 更新时间:2023-10-22 15:15:36 30 4
gpt4 key购买 nike



I'm trying to use an Autorest generated C# client to send MultipartFormDataContent but the request always fails as it seems that the RequestContent in Azure.Core does not seem to support naïve usage.

我正试图使用Autorest生成的C#客户端发送MultipartFormDataContent,但请求总是失败,因为Azure中的RequestContent似乎不支持天真的使用。


For example, I'd like to do something like this:

例如,我想做这样的事情:


RequestContext myRequestContext = new RequestContext();
MultipartFormDataContent formData = myObject.ToMultipartFormDataContent();
RequestContent content = RequestContent.Create(formData);
myAutorestClient.MyEndpointPost(content, myRequestContext);

However, it seems as if the RequestContent.Create call fails to properly convert the MultipartFormDataContent.

然而,RequestContent.Create调用似乎未能正确转换MultipartFormDataContent。


Is there any guidance on how to properly send Multipart form data using the Azure.Core RequestContent object?

关于如何使用Azure.Core RequestContent对象正确发送多部分表单数据,是否有任何指导?


更多回答
优秀答案推荐

Try to use ConvertMultipartFormDataContent method to implement it.

尝试使用ConvertMultipartFormDataContent方法来实现它。


private async Task<RequestContent> ConvertMultipartFormDataContent(MultipartFormDataContent formDataContent)
{
using (MemoryStream ms = new MemoryStream())
{
await formDataContent.CopyToAsync(ms);
byte[] formDataAsBytes = ms.ToArray();
return RequestContent.Create(formDataAsBytes);
}
}

The usage should be like below:

用法应如下所示:


public async Task<Response> Test(MyObject myObject, RequestContext myRequestContext)
{
RequestContext myRequestContext = new RequestContext();
MultipartFormDataContent formData = myObject.ToMultipartFormDataContent();
RequestContent content = await ConvertMultipartFormDataContent(formData);
return await myAutorestClient.MyEndpointPost(content, myRequestContext);
}

更多回答

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com