我需要使用以下格式创建一个POST请求。Net 5:
POST
https://someLMShost.edu/d2l/api/le/{version}/{orgUnit}/content/module/{moduleId}/结构体/
HTTP/1.1
Content-Type: multipart/混合;边界=xxBOUNDARYxx
Content-Llong:{POST正文长度以字节为单位}
--xxBOUNDARYxx
内容类型:应用程序/json
{"IsHidden": false,"IsLocked":false,"短标题":"Test","Type":1,"DueDate":null,"Url":"/content/可扩展性/EXT-104/file.txt","StartDate":null,"TopicType":1,"EndDate":null,"标题":"Test topic content"}
--xxBOUNDARYxx
Content-Dispostion:form-data;name=";filename="file.txt"
Content-Type:text/平原
这是一个带有一些文本内容的示例文本文件
。
--xxBOUNDARYxx--
我的代码如下所示:
var apiResponse = new HttpResponseMessage();
var stringContent = new StringContent(content, Encoding.UTF8, "application/json");
stringContent.Headers.ContentType.CharSet = "";
var stringfile = new StringContent(file, Encoding.UTF8, "text/plain");
stringfile.Headers.ContentType.CharSet = "";
MultipartFormDataContent form = new MultipartFormDataContent
{
stringContent,
{ stringfile, "\"\"", "\"file.txt\"" }
};
apiResponse = await _client.PostAsync(request, form);
我的代码正在生成以下请求:
POST
https://instance.desire2learn.com/d2l/api/le/1.60/329015/content/modules/9885759/structure/?x_a=qwerty
--27d7ac2d-6f96-4c17-84d8-2a93da333bab
内容类型:application/json
内容配置:表单数据
{"标题":"测试主题内容","短标题":"测试","类型": 1,"主题类型":1,"URL":"/content/被强制执行/Sandbox/testHTML.html","StartDate":null,"EndDate":null,"DueDate":null,"IsHidden":false,"IsLocked":false}
--27d7ac2d-6f96-4c17-84d8-2a93da333bab
内容类型:文本/纯
内容处理:表单数据;名称="";文件名="file. txt";文件名*=utf-8""file.txt"
测试
--27d7ac2d-6f96-4c17-84d8-2a93da333bab--
我将如何删除请求中粗体部分?第一部分的“Content-Dispostion: form-data”和第二部分的“;filename*=utf-8”“file.txt”。
可能MultipartFormDataContent不是这种情况的正确内容类型。尝试使用MultipartContent,如下所示。
var stringContent = new StringContent(content, Encoding.UTF8, "application/json");
stringContent.Headers.ContentType.CharSet = string.Empty;
var stringFile = new StringContent(file, Encoding.UTF8, "text/plain");
stringFile.Headers.ContentType.CharSet = string.Empty;
stringFile.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
FileName = file,
Name = "\"\""
};
var multiPartContent = new MultipartContent
{
stringContent,
stringFile
};