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);
}
更多回答
我是一名优秀的程序员,十分优秀!