I need to create a POST request using.Net 5 in the following format:
POST
https://someLMShost.edu/d2l/api/le/{version}/{orgUnit}/content/modules/{moduleId}/structure/
HTTP/1.1
Content-Type: multipart/mixed;boundary=xxBOUNDARYxx
Content-Length: {POST body in length in bytes}
--xxBOUNDARYxx
Content-Type: application/json
{"IsHidden": false, "IsLocked": false, "ShortTitle": "Test", "Type": 1, "DueDate": null, "Url": "/content/extensibility/EXT-104/file.txt", "StartDate": null, "TopicType": 1, "EndDate": null, "Title": "Test topic content"}
--xxBOUNDARYxx
Content-Disposition: form-data; name=""; filename="file.txt"
Content-Type: text/plain
This is a sample text file
with some text content.
--xxBOUNDARYxx--
My code looks like this:
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);
My code is producing the following request:
POST
https://instance.desire2learn.com/d2l/api/le/1.60/329015/content/modules/9885759/structure/?x_a=qwerty&x_b=qwerty&x_c=qwert&x_d=qwerty&x_t=qwert HTTP/1.1
Host: instance.desire2learn.com
traceparent: 00-aff8b6564150364bac900043e5f0daa7-a3512fc259a59c49-00
Content-Type: multipart/form-data; boundary="27d7ac2d-6f96-4c17-84d8-2a93da333bab"
Content-Length: 565
--27d7ac2d-6f96-4c17-84d8-2a93da333bab
Content-Type: application/json
Content-Disposition: form-data
{"Title":"Test topic content","ShortTitle":"Test","Type":1,"TopicType":1,"Url":"/content/enforced/Sandbox/testHTML.html","StartDate":null,"EndDate":null,"DueDate":null,"IsHidden":false,"IsLocked":false}
--27d7ac2d-6f96-4c17-84d8-2a93da333bab
Content-Type: text/plain
Content-Disposition: form-data; name=""; filename="file.txt" ; filename*=utf-8''%22file.txt%22
test
--27d7ac2d-6f96-4c17-84d8-2a93da333bab--
How would I go about removing the bolded parts of the request? "Content-Disposition: form-data" from the first part and "; filename*=utf-8''%22file.txt%22" from the second part.
Probably MultipartFormDataContent is not the right content type for this case. Try using MultipartContent like shown below.
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
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.